亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python3 - Selenium 無法找到提供的 xpath

Python3 - Selenium 無法找到提供的 xpath

蝴蝶不菲 2022-12-27 10:12:19
我正在使用 Python 3 和 Selenium 從網站獲取一些圖像鏈接,如下所示:import sysimport osfrom selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.common.proxy import Proxy, ProxyTypechrome_options = Options()  chrome_options.add_argument("--headless")driver = webdriver.Chrome(chrome_options=chrome_options)driver.get('https://www.sky.com/tv-guide/20200605/4101-1/107/Efe2-364')link_xpath = '/html/body/main/div/div[2]/div[2]/div/div/div[2]/div/div[2]/div[1]/div/div/div[2]/div/img'link_path = driver.find_element_by_xpath(link_xpath).textprint(link_path)driver.quit()解析此 URL 時,您可以在頁面中間看到有問題的圖像。當您在 Google Chrome 中右鍵單擊并檢查元素時,您可以在 Chrome 開發工具中右鍵單擊該元素本身并獲取此圖像的 xpath。在我看來一切都井井有條,但是在運行上面的代碼時出現以下錯誤:Traceback (most recent call last):  File "G:\folder\folder\testfilepy", line 16, in <module>    link_path = driver.find_element_by_xpath(link_xpath).text  File "G:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath    return self.find_element(by=By.XPATH, value=xpath)  File "G:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element    'value': value})['value']  File "G:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute    self.error_handler.check_response(response)  File "G:\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response    raise exception_class(message, screen, stacktrace)selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/main/div/div[2]/div[2]/div/div/div[2]/div/div[2]/div[1]/div/div/div[2]/div/img"}  (Session info: headless chrome=83.0.4103.61)誰能告訴我為什么 Selenium 找不到提供的 xpath?
查看完整描述

4 回答

?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

要提取src圖像的屬性,您需要為 引入WebDriverWaitvisibility_of_element_located()您可以使用以下任一 定位器策略

使用CSS_SELECTOR:


options = webdriver.ChromeOptions() 

options.add_experimental_option("excludeSwitches", ["enable-automation"])

options.add_experimental_option('useAutomationExtension', False)

options.add_argument('--headless')

options.add_argument('--window-size=1920,1080')

driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')

driver.get('https://www.sky.com/tv-guide/20200605/4101-1/107/Efe2-364')

print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.o-layout__item div.c-bezel.programme-content__image>img"))).get_attribute("src"))

使用XPATH:


options = webdriver.ChromeOptions() 

options.add_experimental_option("excludeSwitches", ["enable-automation"])

options.add_experimental_option('useAutomationExtension', False)

options.add_argument('--headless')

options.add_argument('--window-size=1920,1080')

driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')

driver.get('https://www.sky.com/tv-guide/20200605/4101-1/107/Efe2-364')     

print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='o-layout__item']//div[@class='c-bezel programme-content__image']/img"))).get_attribute("src"))

控制臺輸出:


https://images.metadata.sky.com/pd-image/251eeec2-acb3-4733-891b-60f10f2cc28c/16-9/640

注意:您必須添加以下導入:


from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions as EC


查看完整回答
反對 回復 2022-12-27
?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

你有正確的xpath,但不要使用絕對路徑,它很容易被改變。試試這個親戚xpath://div[@class="c-bezel programme-content__image"]//img。


為了實現你的意思,請.get_attribute("src")不要使用.text


driver.get('https://www.sky.com/tv-guide/20200605/4101-1/107/Efe2-364')

element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//div[@class="c-bezel programme-content__image"]//img')))

print(element.get_attribute("src"))

driver.quit()

或者更好的方法,使用 css 選擇器。這應該更快:


element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.c-bezel.programme-content__image > img')))

參考:https ://selenium-python.readthedocs.io/locating-elements.html


查看完整回答
反對 回復 2022-12-27
?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

您的 xpath 似乎是正確的。您無法定位,因為您忘記處理 cookie。自己試試吧。將驅動程序擱置幾秒鐘,然后單擊同意所有 cookie。然后你會看到你的元素。有多種方式來處理cookie。我能夠通過使用我自己的更干凈的 xpath 來找到 xpath。我從最近的父母那里訪問那個元素。



查看完整回答
反對 回復 2022-12-27
?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

如果您在無頭模式下工作,通常增加窗口大小是個好主意。將此行添加到您的選項中:

chrome_options.add_argument('window-size=1920x1080')


查看完整回答
反對 回復 2022-12-27
  • 4 回答
  • 0 關注
  • 279 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號