繁体   English   中英

Python Selenium _ 错误:selenium.common.exceptions.TimeoutException _ 与 WebDriverWait

[英]Python Selenium _ Error: selenium.common.exceptions.TimeoutException _ with WebDriverWait

我正在研究 Selenium 的工作原理。 有些元素已完全加载,但无法点击。 这是案例的例子。 Selenium 需要一段时间才能完全加载。 但是按钮仍然不可点击。 我认为 Selenium 找不到该元素。 我该如何解决这个问题?

仅供参考,当我手动单击按钮时会加载一个弹出窗口。 非常感谢你的协助

url

https://cafe.naver.com/codeuniv

我的 python 代码

from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("https://cafe.naver.com/codeuniv")
time.sleep(1)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="menuLink107"]'))).click()
time.sleep(1)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="upperArticleList"]/table/tbody/tr[1]/td[2]/div/table/tbody/tr/td/a'))).click()

按钮

<a href="#" class="m-tcol-c" onclick="ui(event, 'ftKPZDy0W2UYCbEIxQ-50g',3,'코뮤','30026525','', 'false', 'true', 'codeuniv', 'false', '107'); return false;">코뮤</a>

错误

Traceback (most recent call last):
  File "C:\project\naver.py", line 12, in <module>
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="upperArticleList"]/table/tbody/tr[1]/td[2]/div/table/tbody/tr/td/a'))).click()
  File "C:\project\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 89, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

您可以使用隐式等待来解决您的问题,或者尝试使用 id id 或其他 xpath 或者尝试查找 iframe

wait=WebDriverWait(driver,10)
driver.get("https://cafe.naver.com/codeuniv")
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="menuLink107"]'))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,'cafe_main')))
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="upperArticleList"]/table/tbody/tr[1]/td[2]/div/table/tbody/tr/td/a'))).click()

您的元素在 iframe 中,请稍候并切换到它。

<iframe name="cafe_main" id="cafe_main" title="카페 메인" src="//cafe.naver.com/MyCafeIntro.nhn?clubid=30026525" width="860" height="100%" frameborder="0" scrolling="no" marginwidth="0" marginheight="0" allowtransparency="true" allowfullscreen="" style="height: 2140px;" cd_frame_id_="3e06a515dca2ac262583e8ce3ea0e356"></iframe>

进口:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM