-2

Trying to have selenium click a button on a webpage. Cannot find the element to make it click.

<button type="submit" class="xButton xCTA xSubmit">
  <span>Enter</span>
</button>
Guy
  • 46,488
  • 10
  • 44
  • 88
mrwhit
  • 1
  • 1
  • 2
    Random google for "selenium, find by class": https://www.softwaretestingmaterial.com/how-to-locate-element-by-class-name-locator/ – Taplar Jan 28 '20 at 23:21
  • 1
    Can you add the rest of the html to your question? It will help us to help you better. – RKelley Jan 29 '20 at 00:25

2 Answers2

1
driver.find_element_by_xpath("//button[@class='xButton xCTA xSubmit'].click()
driver.find_element_by_css_selector("button[type='submit']").click()
driver.find_element_by_xpath("//span[contains(text(), 'Enter')]").click()

Any of those should do the trick.

Chris Brocious
  • 161
  • 1
  • 3
  • 13
  • Hi Chris. I keep getting an "element not visible" error, event after putting a 20 second timeout on the page. – mrwhit Jan 29 '20 at 00:03
0

Though the <button> have no or attribute you can still use the other attributes, e.g. class, innerText, etc.

To click() on the element with text as Enter you need to induce WebDriverWait for the element to be clickable() and you can use either of the following Locator Strategies:

  • Using Python and CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.xButton.xCTA.xSubmit[type='submit'] > span"))).click()
    
  • Using Java and XPATH:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='xButton xCTA xSubmit']/span[text()='Enter']"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352