【问题标题】:Selenium Python using ActionChains使用 ActionChains 的 Selenium Python
【发布时间】:2018-12-28 19:34:42
【问题描述】:

我的代码打开一个网站并使用actionChains,它在桌面上右键单击,会弹出一个菜单。我现在需要用 actionChains 再做 3 件事。我需要将鼠标悬停在显示“保存页面我们”的项目上,然后单击弹出的子菜单上的项目,然后按 Enter 按钮。谁能告诉我如何做到这一点?谢谢

from selenium import webdriver

    from selenium.webdriver import ActionChains
    fp = webdriver.FirefoxProfile('/Users/Jake/AppData/Roaming/Mozilla/Firefox/Profiles/emjx467y.default-1524932841911')
    driver = webdriv[enter link description here][1]er.Firefox(fp)
    driver.get('http://www.tradingview.com/screener')
    element = driver.find_element_by_link_text('Screener')
    actionChains = ActionChains(driver)
    actionChains.context_click(element).perform()

【问题讨论】:

  • 屏幕截图指的是 主页 og http://www.google.com,而您的代码试验指的是http://www.tradingview.com/screener。寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及重现它所需的最短代码在问题本身中。没有明确的问题陈述的问题对其他读者没有用处。见:How to create a Minimal, Complete, and Verifiable example.
  • 另外截图上的菜单是浏览器菜单,你不能在 selenium 中与之交互
  • Kiril,有没有办法使用坐标点击按钮?谢谢

标签: python selenium


【解决方案1】:

通过使用此行:actionChains.context_click(element).perform(),您正在尝试右键单击 Screener 菜单。但理想的行为应该是悬停,然后从 3 个选项中选择一个。

我选择外汇筛选器,您可以根据自己的需要选择任何一个。

对于悬停,您可以尝试以下代码:

actionChains.move_to_element(element).perform()  

完整的代码是这样的:

driver.get("http://www.tradingview.com/screener")  
wait = WebDriverWait(driver,40)

driver.find_element_by_css_selector("span[class*='tv-header__dropdown-wrap--noarrow'] span[class$='lang-icon--current-lang']").click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='English (UK)']/parent::a"))).click()
element = driver.find_element_by_link_text('Screener')
actionChains = ActionChains(driver)
actionChains.move_to_element(element).perform()

wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Forex Screener"))).click()  

确保导入这些:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.action_chains import ActionChains

【讨论】:

  • 你所做的非常整洁,但不幸的是,这不是我需要的。我需要使用坐标点击插件按钮。
猜你喜欢
  • 2021-03-12
  • 1970-01-01
  • 2020-03-18
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多