繁体   English   中英

如何自动在Google Chrome浏览器中打开新标签页? 硒Python

[英]How to automate opening of new tabs on Google Chrome? Selenium Python

自动化任务需要移至打开的选项卡,执行命令(单击按钮),然后移至下一个选项卡。 接下来的4-5个选项卡将重复此过程。

我已经有一个可以自动执行此过程的代码。 我有一个for循环,该循环遍历打开的选项卡的每个窗口句柄并自动执行按钮单击。 但是问题在于,在每个选项卡上, 驱动程序都等待按钮单击的执行过程进行处理,并等待新页面加载,然后再进入下一个选项卡 理想情况下,我希望驱动程序单击按钮并立即移动到下一个选项卡,而无需等待新页面加载。

有什么方法可以做到这一点? 除了使用Selenium进行这种自动化之外,还有其他选择吗?

我当前的代码如下所示:

handles = driver.window_handles
for i in range(4):
    driver.switch_to_window(handles[i])
    driver.find_element_by_id('submit').click()

for i in range(3):
    driver.switch_to_window(driver.window_handles[i+1])

    chain = ActionChains(driver)
    element = driver.find_element_by_name('submit')
    chain.move_to_element_with_offset(element, 0, 0)
    chain.click(element)
    chain.release(element)
    chain.perform()

我使用了详细代码来使用ActionChains单击按钮。 但是即时通讯收到StateElementReferenceException。 rrror在chain.perform()处触发

StaleElementReferenceException:消息:陈旧元素引用:元素未附加到页面文档

使用动作链执行单击动作将使Selenium继续运行,而不必等待单击动作的结果。 在linux,python3.4,chromedriver2.38上进行了测试:

from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import *

chain = ActionChains(super().driver)

try:
    element = driver.find_element_by_id('submit')
    chain.move_to_element_with_offset(element, 0, 0)
    chain.click(element)
    chain.release(element)
    # Perform the chained actions including the left-click.
    chain.perform()
except:
    print("Failed to click element")
    raise

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM