作为 Selenium 的新手,我的第一次尝试是保存页面上的所有链接。我的代码来自一个我无法工作的示例。 我做错了什么?
这是我的代码:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = "https://www.python.org"
driver = webdriver.Chrome()
driver.get(url)
driver.minimize_window()
links = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By. LINK_TEXT, "a")))
for link in links:
href = link.get_attribute("href")
if href is not None:
print(href)
driver.quit()
另外,如何保存链接并能够在需要时单击它们。
这是我收到的错误消息。
Traceback (most recent call last):
File "/media/joe-2/Ubuntu-Storage1/Exuma-Snoops/RGD-code/link-test.py", line 21, in <module>
links = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By. LINK_TEXT, "a")))
File "/home/joe-2/.local/lib/python3.10/site-packages/selenium/webdriver/support/wait.py", line 105, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
我的操作系统是Ubuntu 22.04
links = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By. LINK_TEXT, "a")))
将上面的行更改如下:
links = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//a")))