【问题标题】:Fails to perform keys down and hit enter in context menu python selenium无法在上下文菜单python selenium中执行按键并按Enter
【发布时间】:2019-08-27 14:22:44
【问题描述】:

我想登录一个网站。右键单击其中一个链接并在新选项卡或新窗口中打开。

我早先在这里搜索过,并在此处发布之前用谷歌搜索过。可能是我做错了

button=browser.find_element_by_link_text('Menus');
action=ActionChains(browser)
action.context_click(button).perform() #--> Till here working fine, Right clicks on Menu
action.send_keys(Keys.ARROW_DOWN+Keys.ARROW_DOWN+Keys.ENTER).perform() #--> Not Working

【问题讨论】:

  • 感谢您的拖延,这让我更加好奇并开始寻找其他地方。我发现使用 pyautogui。 ``` python import pyautogui button=browser.find_element_by_link_text('Menus'); action=ActionChains(browser) action.context_click(button).perform() #--> 直到这里工作正常,右键单击菜单 pyautogui.typewrite(['down','enter']) ``

标签: python python-3.x selenium-webdriver contextmenu


【解决方案1】:

我不会朝那个方向发展,因为当你成为 running your Selenium tests in Parallel Mode 时,执行上下文菜单点击会咬你一口

我宁愿推荐:而不是打开上下文菜单并单击:

  1. 从链接中提取href attribute
  2. 使用Window.open() JavaScript 函数在新标签页中打开链接
  3. 使用Explicit Wait 一直哭到窗口数变为2
  4. Switch 新标签的上下文

示例代码:

button = browser.find_element_by_link_text('Menus')
href = button.get_attribute("href")
browser.execute_script("window.open('" + href + "')")
WebDriverWait(browser, 10).until(EC.number_of_windows_to_be(2))
browser.switch_to.window(browser.window_handles[1])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 2016-10-16
    相关资源
    最近更新 更多