简体   繁体   English

Selenium WebDriver即使看起来似乎也无法单击元素

[英]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): 在此页面上 ,使用Python 2,使用Firefox的 Selenium,我正在尝试让驱动程序单击以下按钮(放大镜):

在此处输入图片说明

<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"]' : 我将以下代码用于元素的XPath, 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 . 硒的常见的例外EC.element_to_be_clickable没有标识的元素,也不visibility_of_element_located也不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. 然而,奇怪的是,在某些情况下,驱动程序实际上已经能够识别该元素,然后执行driver.find_element_by_xpath(x)似乎可以找到XPath和.click()元素。 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. 一秒钟我以为脚本会在页面加载之前快速执行该操作,但是5秒钟WebDriverWait足以加载页面,在此之前我还有额外的页面加载睡眠。

The element doesn't seem to be in an IFrame. 该元素似乎不在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). 我正在运行最新版本的Firefox(61.0),Selenium(3.13)和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 ? 如果我用By.ID而不是By.XPATH获取元素,这行得通,也许您输入了错误的xpath

if a use xpath this also work x = '//*[@id="search-btn"]' 如果使用xpath也可以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: 使用Firefox作为驱动程序,我能够通过执行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? 问题在于Selenium驱动程序无法识别XPath元素,这是由于当前版本的geckodriver(0.21.0)与Selenium(3.13.0)结合存在错误 ,请参阅: 出现以下问题时 ,Selenium webdriver管道错误:命令之间的差距?

I downgraded to geckodriver 0.20.1 to avoid the problem. 我降级到geckodriver 0.20.1以避免该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Selenium WebDriver即使存在也无法一致选择元素 - Python selenium webdriver not consistently selecting element even though it's there Selenium Webdriver找不到单击按钮的元素 - Selenium Webdriver Cannot find the element of the click button Selenium 无法找到 html 元素,即使它是可见的 - Selenium fails finding html element even though its visible 即使存在,也无法在 Selenium 中找到第二个元素 - Cannot find second element in Selenium even though it exists 硒2- python-即使硒是最新的,也无法导入webdriver - selenium 2- python- cannot import webdriver even though selenium is up to date 带有 Python 的 Selenium Webdriver:元素不可点击且无法读取 null 的属性“点击” - Selenium Webdriver with Python: Element is not clickable & Cannot read property 'click' of null 元素是可见的,但webdriver无法找到 - Element is visible but cannot be found by webdriver 即使看起来正确也无法创建表面 - cannot create surface even though it seems correct 如何使用 Selenium WebDriver 单击该元素 - How to click that element with Selenium WebDriver 使用Selenium和python,即使元素存在但实际上不可见,如何检查 - Using selenium and python, how to check that even though an element exists but it is not actually visible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM