我是使用 python 和 selenium 的新手,我正在嘗試從網站獲取影像源。我實際上是通過標簽名稱查找元素,但我不知道這是否正確。無論如何它給了我一個錯誤:
InvalidArgumentException: Message: invalid argument: 'using' must be a string
在這下面是我寫的代碼
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
PATH = Service("/Users/fscozano/documenti/chromedriver-2.exe")
print("setup")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://apod.nasa.gov/apod/random_apod.html")
tag = driver.find_element((By.TAG_NAME, "b")).getText()
print(tag)
driver.quit()
錯誤在:
tag = driver.find_element((By.TAG_NAME, "b")).getText()
您可以親自檢查該網站,因為我真的不知道如何解決這個問題,感謝每種型別的幫助:)
uj5u.com熱心網友回復:
影像源在 a 內,<iframe>因此您必須:
Induce WebDriverWait等待所需的框架可用并切換到它。
Induce WebDriverWait使所需元素可點擊。
您可以使用以下定位器策略:
driver.get("https://apod.nasa.gov/apod/random_apod.html") WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://apod.nasa.gov/apod']"))) print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h1[normalize-space()='Astronomy Picture of the Day']//following::p[2]//a/img"))).get_attribute("src"))控制臺輸出:
https://apod.nasa.gov/apod/image/2111/MACSJ0138_Hubble_1080.jpg注意:您必須添加以下匯入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
參考
您可以在以下位置找到一些相關討論:
- iframe下#document的處理方式
- 通過Selenium和python切換到iframe
- selenium.common.exceptions.NoSuchElementException:訊息:沒有這樣的元素:嘗試使用硒單擊“下一步”按鈕時無法定位元素
- python中的硒:NoSuchElementException:訊息:沒有這樣的元素:無法定位元素
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/391812.html
標籤:Python 硒 硒网络驱动程序 路径 网络驱动程序等待
上一篇:如何找到“著作權”文本?
