首页 > 解决方案 > 如何使用 Selenium 从右键菜单中选择选项

问题描述

我使用chrome作为驱动程序,双击/上下文单击后,提示窗口打开,但驱动程序不会切换到提示窗口。这是我尝试过的...我打开的页面是google.com,搜索,然后尝试右键单击,以便我可以在不同的选项卡中打开结果。提前致谢。

.......
element = driver.find_element_by_class_name("LC20lb")
actionchains = ActionChains(driver)
actionchains.context_click(element).perform()
# Driver needs to switch to the popup from here before it can press the down arrow.
sleep(5)
actionchains.send_keys(Keys.ARROW_DOWN).perform()
sleep(4)
driver.quit()

标签: pythonseleniumautomationwebdriverselenium-chromedriver

解决方案


使用 pyautogui,您可以在网页上下文之外按向下箭头。下面将选择 context minu 的第一个选项。尝试这个:

element = driver.find_element_by_class_name("LC20lb")
actionchains = ActionChains(driver)
actionchains.context_click(element).perform()
# Driver needs to switch to the popup from here before it can press the down arrow.
sleep(5)
#actionchains.send_keys(Keys.ARROW_DOWN).perform()
import pyautogui
pyautogui.press('down')
pyautogui.press('enter')
sleep(4)
driver.quit()

推荐阅读