如何使用Selenium从右击菜单中选择选项

2024-04-19 09:51:52 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Tags: thetoby网站driver驱动程序sleep页面
3条回答

使用pyautogui,您可以在网页上下文之外按向下箭头。下面将选择上下文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()

这是我试过的。你知道吗

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

在上面的代码中,您可以使用WindowHandles在窗口之间导航,然后在窗口上获取执行操作所需的驱动程序操作。你知道吗

.......
element = driver.find_element_by_class_name("LC20lb")
actionchains = ActionChains(driver)


window_before = driver.window_handles[0];  - this is for the first window.
actionchains.context_click(element).perform()
window_after = driver.window_handles[1];  - this is for the second window.
driver.switch_to.window(window_after);  - switching the driver to the window that the action needs to be performed.


actionchains.send_keys(Keys.ARROW_DOWN).perform()
sleep(4)
driver.quit()

希望这有帮助!!!!你知道吗

从你所描述的,它不是一个弹出窗口。。。这是一个上下文菜单。上下文菜单是特定于浏览器的,因此不能使用Selenium与之交互。有其他方法可以做到这一点,而不诉诸上下文菜单。例如,您不必右键单击某个链接,而是可以获取链接的href(标记),打开一个新窗口,并将该窗口导航到从href检索到的URL。你知道吗

相关问题 更多 >