首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python + Selenium TimeoutException

Python + Selenium TimeoutException
EN

Stack Overflow用户
提问于 2022-02-22 14:08:15
回答 1查看 641关注 0票数 1

这是我第一次使用Python和Selenium。代码的第一部分可以工作,但是当它转到第二页时,它永远找不到任何元素。如果我先翻转代码并让它转到第二个站点,它就能工作。我在这里做错什么了?我尝试了xpath,CSS_Selector,Class_Name似乎什么都不起作用。这是我得到的错误:

回溯(最近一次调用):文件"C:\Users\dresd\PycharmProjects\Test2\main.py",第20行,在click_Register =WebDriverWait(click_Register=WebDriverWait中)(驱动程序,"q-text qu-省略性qu-whiteSpace nowrap“)).click().click()文件"C:\Users\dresd\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\support\wait.py",第89行,在click_Register(消息、屏幕、selenium.common.exceptions.TimeoutException:消息:

代码语言:javascript
复制
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")

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-22 14:14:53

你必须将驱动程序切换到新打开的选项卡。

否则,焦点将停留在第一个浏览器窗口上。

TimeoutException实际上意味着Selenium无法通过传递的定位器定位元素。

另外,您正在使用的定位器也是错误的。

试试这个:

代码语言:javascript
复制
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")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71222773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档