如何使用硒从右键菜单中选择选项

问题描述 投票:0回答:3

我使用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()
python selenium automation webdriver selenium-chromedriver
3个回答
0
投票

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

0
投票

根据您的描述,这不是弹出窗口,而是上下文菜单。上下文菜单是特定于浏览器的,因此无法使用Selenium进行交互。还有其他方法可以执行此操作而无需借助上下文菜单。例如,除了右键单击链接之外,您还可以获取链接的href(A标记),打开一个新窗口,然后将该窗口导航到从href检索到的URL。


0
投票

这是我尝试过的。

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

希望有帮助!!!!

© www.soinside.com 2019 - 2024. All rights reserved.