簡體   English   中英

Selenium Webdriver (Python) - 無法定位網頁的任何元素

[英]Selenium Webdriver (Python) - Unable to locate ANY element of the webpage

我正在嘗試瀏覽此網頁 ( https://www.msci.com/end-of-day-data-country ),從下拉菜單中選擇一些選項,然后進一步單擊每個國家/地區的名稱以轉到不同的網頁.

但是,我無法從頁面中找到任何內容,通過 id、類、名稱、xpath 進行搜索,也無法找到或切換到任何 iframe。

起初,我試圖通過復制其 xpath 來找到貨幣下拉列表:

!pip install selenium
!apt-get update # to update ubuntu to correctly run apt install
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
import sys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
wd = webdriver.Chrome('chromedriver',chrome_options=chrome_options)

# Example searches
wd.get("https://www.msci.com/end-of-day-data-country")
wd.find_element_by_xpath("//*[@id='templateForm:selectOneMenuCategoryCountryCurrency']")
wd.find_element_by_css_selector("#templateForm\:selectOneMenuCategoryCountryCurrency")

然后我發現問題可以鏈接到 iframe 在這里,但嘗試並未能找到 iframe:

wd.find_elements_by_tag_name("iframe")

如何正確訪問此頁面中的內容?

我有一些建議給你

  • 首先,我真的建議您從chrome Option中刪除headless參數,以便查看發生了什么。 也許某些彈出窗口或通知會阻止您訪問您的元素。

  • 此外,您需要首先使用wd.get("https://www.msci.com/end-of-day-data-country")訪問網頁,我相信您知道它

  • 最后,貨幣下拉ID是明確的。 所以你可以很容易地得到它。 但是您可能會注意到有些元素很難與之交互。 所以使用一些 JavaScript 來鋪平道路是一個很好的做法。 看一下這個:

     curr_drop_down= wd.find_element_by_id("templateForm:selectOneMenuCategoryCountryCurrency") wd.execute_script("arguments[0].click();", curr_drop_down)

當我導航到你的 URL 時,我沒有得到你想要的頁面。 我被重定向到“接受條款和條件”頁面:

在此處輸入圖片說明

您將通過 selenium 獲得此功能,因為當您啟動 selenium 時,它每次都會創建一個新的用戶配置文件。 這意味着不使用您的設置、cookies 和緩存。

您可以通過啟動新的隱身會話來模擬此行為。

在找到任何對象之前,您需要按 cookie 消息上的接受並接受條款和條件頁面。

我還建議您在頁面中添加一些同步功能。 您可以在此處閱讀有關 selenium 等待策略的信息

然后,您對腳本中的 iframe 是正確的。 你可以這樣做:

wd.switch_to_frame(<locator>)

或者(推薦)使用更穩定的等待+預期條件;

WebDriverWait(wd, 10).until(EC.frame_to_be_available_and_switch_to_it((<locator>))

把它們放在一起給你這個:

wd.get("https://www.msci.com/end-of-day-data-country")

# Accept cookies
cookieAccept = WebDriverWait(wd, 10).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(@class,'gdpr-allow-cookies')]")))
cookieAccept.click()

#wait for the message to dribble off screen....
WebDriverWait(wd, 10).until_not(EC.element_to_be_clickable((By.XPATH,"//button[contains(@class,'gdpr-allow-cookies')]")))

# Accept on Ts&Cs
ConditionsAccept = WebDriverWait(wd, 10).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(@class,'accept-btn')]")))
ConditionsAccept.click()


#switch to the frame:
WebDriverWait(wd, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[contains(@src,'IEIPerformanceCountry.jsf')]")))

#get the currency object as per your code
currency = WebDriverWait(wd, 10).until(EC.visibility_of_element_located((By.XPATH,"//*[@id='templateForm:selectOneMenuCategoryCountryCurrency']")))
print(currency.text)

由於我只是打印貨幣輸出,我得到:

USD EUR 本地 GBP JPY CAD CHF HKD AUD MXN

最后,當您完成框架並返回“主”頁面/框架時,請使用:

wd.switch_to_default_content()

暫無
暫無

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

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