我想同时打开和登录 5 个选项卡,而在选项卡之间没有延迟。我试过了:
import threading
import time
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def openurl(threadId):
print(threading.currentThread().getName(),' Thread')
url = ('https://www.facebook.com/')
print(url)
driver.execute_script("window.open('{0}')".format(url))
#driver.title(threadId)
time.sleep(0.1)
driver.set_window_size(920, 680)
driver.find_element(By.ID, "email").send_keys("xx")
driver.find_element(By.ID, "pass").send_keys("yy")
driver.find_element(By.ID, "loginbutton").click()
if __name__=='__main__':
driver = webdriver.Chrome()
windows_before = driver.current_window_handle
for i in range(5):
t1 = threading.Thread(name=i,target=openurl, args=(i,))
t1.start()
t1.join()
Run Code Online (Sandbox Code Playgroud)
但它正在抛出:
回溯(最近一次调用):文件“C:\Users\1024983\AppData\Local\Programs\Python\Python37\lib\threading.py”,第 870 行,运行 self._target(*self._args, ** self._kwargs) 文件“C:\Users\1024983\AppData\Local\Programs\Python\Python37\fb-thread.py”,第 30 行,在 openurl driver.find_element(By.ID, "email").send_keys( “xx”)文件引发异常类(消息,屏幕,堆栈跟踪)selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{“方法”:“css选择器”,“选择器”:“[id ="email"]"}(会话信息:chrome=78.0.3904.108)
如果我增加睡眠时间,标签之间会有延迟。我尝试使用driver.title进行导航,但所有选项卡的案例标题都相同。