【问题标题】:How to click on an element through Selenium ActionChain and Python如何通过 Selenium ActionChain 和 Python 点击​​元素
【发布时间】:2019-05-10 21:39:36
【问题描述】:

我是 Python Selenium 的新手。我被这个困住了。请帮助我找到解决方案。 我正在尝试使用 ActionChain 单击此 MENU1。

locator = (By.XPATH, "//div[@title='MENU1']")
text_element = WebDriverWait(driver, 20).until(visibility_of_element_located(locator))
actions = ActionChains(driver)
actions.move_to_element(text_element).click().perform()

以下是 HTML:

<div _ngcontent-c0="" class="hyd-group-tree-node-label active" tooltipposition="bottom" title="MENU1">
    <span _ngcontent-c0="" class="ui-treenode-inner-icon fa fa-fw fa-building"></span>
    MENU1
</div>

一切运行良好。没有错误。 它移动到元素,但我看不到点击移动。 我不确定我错过了什么。我尝试使用text_element.click(),但它不起作用。我在 Chrome 和 Firefox 中都试过了。

谢谢

【问题讨论】:

  • 抱歉打错了。我已经编辑了。

标签: python selenium-webdriver xpath css-selectors webdriverwait


【解决方案1】:

所需元素是 Angular 元素,因此调用 click() 您需要诱导 WebDriverWait 以使所需的 元素可点击,您可以使用以下解决方案:

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.hyd-group-tree-node-label.active[title='MENU1']>span.ui-treenode-inner-icon.fa.fa-fw.fa-building"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='hyd-group-tree-node-label active' and @title='MENU1']/span[@class='ui-treenode-inner-icon fa fa-fw fa-building']"))).click()
    

如果您仍想使用 ActionChains,您可以使用以下任一解决方案:

  • 使用CSS_SELECTOR

    ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.hyd-group-tree-node-label.active[title='MENU1']>span.ui-treenode-inner-icon.fa.fa-fw.fa-building")))).click().perform()
    
  • 使用XPATH

    ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='hyd-group-tree-node-label active' and @title='MENU1']/span[@class='ui-treenode-inner-icon fa fa-fw fa-building']")))).click().perform()
    
  • 注意:您必须添加以下导入:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 2022-01-17
    • 2020-04-09
    • 2020-11-06
    • 2020-04-28
    • 2018-08-16
    • 1970-01-01
    相关资源
    最近更新 更多