简体   繁体   中英

Selenium WebDriver cannot click element even though it seems to be visible

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?

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() 

可以使用以下方法来定位<img>标记,而不是使用<button>元素进行更细化的处理:

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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM