python - 使用 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
我该如何解决?
更新
我注意到,如果我使用调试器并慢慢地跳过,那么就没有错误,它会加载所有页面并单击所有链接。我认为几乎同时在多个标签中是主要问题。请让我知道你的建议。
解决方案
推荐阅读
- regex - 查找所有出现 1-3 次的字符
- optaplanner - Optaplanner 7.9.0 和添加多线程:相同的planningId 异常
- javascript - 使用 PHP 通过电子邮件发送 Javascript 对象
- sql - 为什么 SQL Server 内置 IntelliSense 无法正确找到表名?
- python - Extracting blocks from a text file
- android - XAxis 上 MPAndroidChart ValueFormatter 的奇怪行为
- django - django错误管理表单丢失或被篡改
- c++ - 向量成员已重置且无法访问
- javascript - Impossible to found the right page with Nuxt and Vuejs
- android - 当连接再次可用时启动任务的最佳方式是什么?(API 24 或更高版本)