首页 > 解决方案 > 使用 selenium 同时单击多个选项卡中的链接

问题描述

我正在使用以下代码来调用 selenium 以打开多个选项卡并同时单击每个选项卡上的特定链接。

输入文件google-search-terms.adoc包含:

5 Dysfunctions of a Team by Patrick Lencioni
Agile Metrics in Action: How to measure and improve team performance
Agile Testing : A Practical Guide for Testers and Agile Teams
Building Great Software Engineering Teams by Josh Tyler
Building Team Power: How to Unleash the Collaborative Genius of Teams for Increased Engagement, Productivity, and Results, by Thomas Kayser

编码

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import urllib.parse
import time
from multiprocessing import Process

start = time.time()

caps = DesiredCapabilities().CHROME
caps["pageLoadStrategy"] = "eager"   # Do not wait for full page load

browser = webdriver.Chrome(desired_capabilities=caps)

def worker(ii):
    browser.switch_to.window(ii)
    try:
        result = browser.find_elements_by_xpath('//div[@id="rso"]/div/div')[0]
        result.find_element_by_xpath("./div/a").click()
    except:
        print("An exception occurred")


all_procs = []
for x in range(1, len(browser.window_handles)):
    p = Process(target=worker, args=(browser.window_handles[x],))
    all_procs.append(p)
    p.start()

for p in all_procs:
    p.join()

print("Total time taken: ", time.time()-start)

现在它正在抛出错误

p = Process(target=worker, args=(browser.window_handles[x],))
TypeError: 'NoneType' object is not subscriptable

我该如何解决?

更新

我注意到,如果我使用调试器并慢慢地跳过,那么就没有错误,它会加载所有页面并单击所有链接。我认为几乎同时在多个标签中是主要问题。请让我知道你的建议。

标签: pythonpython-3.xseleniumselenium-webdriver

解决方案


推荐阅读