如何使用 Selenium 从右键菜单中选择选项 [英] How to selecting option from right click menu with Selenium

查看:124
本文介绍了如何使用 Selenium 从右键菜单中选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using chrome as the driver and after double-clicking/context-clicking, the prompt window opens but the driver won't switch to the prompt window. Here is what I have tried... The page I am opening is google.com, search, then trying to right-click so i can open the results in different tabs. Thanks in advance.

.......
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()

推荐答案

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

With pyautogui you can press the down arrow outside of the context of the web page. Below will select the first option of the context minu. Try this:

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()

这篇关于如何使用 Selenium 从右键菜单中选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆