簡體   English   中英

Python 2.7 Selenium 網站上沒有這樣的元素

[英]Python 2.7 Selenium No Such Element on Website

我正在嘗試從博彩網站進行一些網絡抓取:

作為過程的一部分,我必須點擊左側“收藏夾”部分下的不同按鈕來選擇不同的比賽。

我們以 ENG Premier League 按鈕為例。 我將按鈕標識為:

截屏
(來源: 666kb.com

XPath 為: //*[@id="SportMenuF"]/div[3] ,ID 為 91。

我點擊按鈕的代碼如下:

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


chrome_path = "C:\Python27\Scripts\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)

driver.get("URL Removed")

content = driver.find_element_by_xpath('//*[@id="SportMenuF"]/div[3]')
content.click()

不幸的是,我在運行腳本時總是收到此錯誤消息:

"no such element: Unable to locate element: 
{"method":"xpath","selector":"//*[@id="SportMenuF"]/div[3]"}" 

我嘗試了不同的標識符,例如 CCS Selector、ID 以及如上例所示的 Xpath。 我也嘗試使用等待和顯式條件。 這些都沒有奏效。

我還嘗試從網站上抓取一些值,但沒有成功:

from selenium import webdriver
from selenium.webdriver.common.by import By

chrome_path = "C:\Python27\Scripts\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)

driver.get("URL removed")


content = driver.find_elements_by_class_name('price-val')

for entry in content:
    print entry.text

同樣的問題,什么都不顯示。

該網站嵌入了來自不同網站的 iframe。 這可能是我問題的原因嗎? 我也嘗試直接從 iframe URL 中抓取,但也沒有用。

我將不勝感激任何建議。

有時元素要么隱藏在 iframe 后面,要么還沒有加載

對於 iframe 檢查,請嘗試:

driver.switch_to.frame(0)

對於等待檢查,請嘗試:

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

element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, '-put the x-path here-')))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM