这是我第一次使用Python和Selenium。代码的第一部分可以工作,但当它转到第二页时,它永远找不到任何元素。如果我翻转代码,让它先转到第二个网站,它就可以工作了。我在这里做错了什么?我试过xpath,CSS_Selector,Class_Name似乎什么都不起作用。这是我得到的错误:
追踪(最近一次通话(:文件";C: \Users\dresd\PycharmProjects\Test2\main.py";,第20行,inclick_Register=WebDriverWait(驱动程序,20(。直到(EC.presence_of_element_located((By.CLASS_NAME,"q-text qu省略号qu whiteSpace--nowrap"((。click((文件";C: \Users\dresd\AppData\Local\Programs\Python310\lib\site-packages\selenium\webdriver\support\wait.py",第89行,直到引发超时异常(消息、屏幕、堆栈(selenium.com.mon.exceptions.TimeoutException:消息:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome ("C:/chromedriver.exe")
driver.get("https://10minutesemail.net/")
Copy_Email = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.ID, "copyEmailAddress"))).click()
time.sleep(10)
driver.execute_script("window.open('https://quora.com/','_blank')")
click_Register = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.CLASS_NAME, "q-text qu-ellipsis qu-whiteSpace--nowrap"))).click()
name = driver.find_element(By.NAME, "profile-name")
email = driver.find_element(By.ID, "email")
name.send_keys("Jackson Fuller")
提前感谢!
您必须将驱动程序切换到新打开的选项卡。
否则,焦点将保持在第一个浏览器窗口上TimeoutException
实际上意味着Selenium无法通过传递的定位器定位元素
此外,您正在使用的定位器也不好
试试这个:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome ("C:/chromedriver.exe")
driver.get("https://10minutesemail.net/")
Copy_Email = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.ID, "copyEmailAddress"))).click()
time.sleep(10)
driver.execute_script("window.open('https://quora.com/','_blank')")
driver.switch_to.window(driver.window_handles[1])
click_Register = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.CLASS_NAME, "q-text qu-ellipsis qu-whiteSpace--nowrap"))).click()
name = driver.find_element(By.NAME, "profile-name")
email = driver.find_element(By.ID, "email")
name.send_keys("Jackson Fuller")