【问题标题】:How to click on this button with Selenium?如何使用 Selenium 单击此按钮?
【发布时间】:2019-06-10 02:13:37
【问题描述】:

这是我尝试根据“检查”点击的元素的所有部分:

<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
    ::before
    <div class="ui-dialog-buttonset">
        <button type="button" class="done ui-button ui-corner-all ui-widget">
        Done</button>
    </div>
    ::after
</div>

当我想点击它时,我假设它位于::before 部分,因为它正在显示并可点击。在代码中,我确保滚动并等待两秒钟,然后单击以确保按钮可见,但我得到:

selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable

但我不明白它是如何不可交互的。当我在检查器中将鼠标悬停在它上面时,&lt;button&gt;...&lt;/button&gt; 中的所有内容都会突出显示,所以我要单击的按钮必须在那里,对吧?

以下是我尝试过的几件事:

browser.find_element_by_css_selector('button[type=button]').click()

browser.find_elements_by_xpath("//*[contains(text(), 'Done')]").click()

# The above returns a list for some reason?

browser.find_elements_by_css_selector('done.ui-button.ui-corner-all.ui-widget')

我希望我能记住我尝试过的所有事情,但无论如何我希望有人能帮助我。

【问题讨论】:

  • 尽可能显示你的源页面,否则其他人无法调试帮助。此外,selenium 可以执行脚本,您可以尝试使用 javascript 或 jquery 脚本来使元素可交互,甚至可以使用它们完成所有步骤。
  • 当你使用.find_elements(注意复数)时,它会返回一个列表而不是像.find_element(单数)那样的单个元素。你试过browser.find_element_by_xpath("//button[.='Done']")吗?如果您在这些.find_elements 呼叫中获得len()... 是1?我想知道与您正在使用的定位器匹配的按钮是否不超过一个,并且第一个不可见,但您想要第二个或第三个,等等。
  • @JeffC Jeff 我的朋友,你真的是我的英雄。我在该列表中单击了一个特定元素,它工作得非常好。用这个回答这个问题,这样你我就可以给你一个复选标记来给我答案。你值得拥有一切。
  • @jeepersmcface ,请注意在ElementNotVisibleException 的情况下,这种方法(使用find_elements_..)应仅用于检查选择器匹配的元素数量,而不是通过索引选择所需元素。您仍然应该搜索唯一选择器。
  • 同意@Andersson。我猜表格中有多个按钮,每个按钮都在不同的行上。您应该能够根据表中的值为给定按钮构造 XPath。这是执行此操作的更好方法……我的评论变成了答案,更像是一个调试步骤。好吧,它不一定是表格中的一行,这只是一个例子......

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

要单击元素,因为所需元素是 动态 元素,您必须诱导 WebDriverWait 以使 元素可点击 并且您可以使用以下任一解决方案:

  • 使用CSS_SELECTOR

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix"))).click()
    
  • 使用XPATH

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='ui-dialog-buttonpane ui-widget-content ui-helper-clearfix']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

您可以在selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable using Selenium找到详细讨论

【讨论】:

  • 嗯...我得到 selenium.common.exceptions.TimeoutException: Message:
  • 查看更新的答案并让我知道状态
【解决方案2】:

您可以使用 ActionChains 移动到元素

from selenium.webdriver.common.action_chains import ActionChains
element = driver.find_elements_by_css_selector("div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix")

actions = ActionChains(driver)
actions.move_to_element(element).perform()

或者你可以使用 scrollIntoView() 滚动直到元素在视图中:

driver.execute_script("arguments[0].scrollIntoView();", element)

【讨论】:

    【解决方案3】:

    当您使用.find_elements(注意复数)时,它将返回一个列表,而不是像.find_element(单数)那样返回单个元素。你试过了吗

    browser.find_element_by_xpath("//button[.='Done']")
    

    如果您在这些.find_elements 调用中得到len()... 是1?我想知道与您正在使用的定位器匹配的按钮是否不超过一个,并且第一个不可见,但您想要第二个或第三个,等等。

    【讨论】:

      猜你喜欢
      • 2019-12-14
      • 1970-01-01
      • 2020-02-24
      • 2021-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多