Reputation: 5922
Using Python 2, Selenium using Firefox, on this page, I am trying have the driver click the following button (the magnifying glass):
<button id="search-btn" type="button" class="header__user-menu-item header__search-btn">
<span class="sr-only">Search</span>
<img src="/sites/default/themes/custom/smp_bootstrap/images/search.svg" class="header__user-menu-icon fa fa-search fa-fw" alt="Search">
</button>
I use the following code for the XPath of the element, x = '//*[@id="search-btn"]'
:
x = '//*[@id="search-btn"]'
try:
element = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, x)))
except:
print "Element not clickable"
else:
found_element = driver.find_element_by_xpath(x)
try:
found_element.click()
except:
raise
Selenium's common exception EC.element_to_be_clickable
doesn't identify the element, nor does visibility_of_element_located
nor presence_of_element_located
.
Strangely however, at a few occasions the driver has actually been able to identify the element and then perform driver.find_element_by_xpath(x)
which appears to find the XPath and .click()
the element. At that point everything works. For a second I thought that the script was to quick to execute the operation before the page had loaded, but 5 seconds WebDriverWait
is plenty to load the page, and I have an additional page-load-in sleep before that.
The element doesn't seem to be in an IFrame. I have already passed through the "Accept Conditions"-buttons, etc.
I am running the latest versions of Firefox (61.0), Selenium (3.13) and Geckodriver (0.21.0).
What could be the problem here?
Upvotes: 0
Views: 1379
Reputation: 5922
Using Firefox as the driver, I was able to click the element by executing JavaScript:
driver.execute_script("window.document.getElementById('search-btn').click()")
Note that the above is a non-conventional measure, which shouldn't be needed. The other answers are correct and the usual way to do it.
The problem resides with the Selenium driver being unable to identify XPath elements, due to a bug in the current version of geckodriver (0.21.0) combined with Selenium (3.13.0), see: Broken pipe error selenium webdriver, when there is a gap between commands?
I downgraded to geckodriver 0.20.1 to avoid the problem.
Upvotes: 1
Reputation: 193088
Instead of targeting the <button>
element for a more granular approach you can target the <img>
tag as follows:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//img[@class='header__user-menu-icon fa fa-search fa-fw' and @alt='Search']"))).click()
Upvotes: 1
Reputation: 7402
if i get element with By.ID
not with By.XPATH
this works, maybe you give wrong xpath
?
if a use xpath
this also work x = '//*[@id="search-btn"]'
id = 'search-btn'
element = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.ID, id)))
element.click()
Upvotes: 1