【问题标题】:Selenium Webdriver (Python) - Unable to locate ANY element of the webpageSelenium Webdriver (Python) - 无法找到网页的任何元素
【发布时间】:2020-09-27 11:28:40
【问题描述】:

我正在尝试浏览此网页 (https://www.msci.com/end-of-day-data-country),从下拉菜单中选择一些选项,然后进一步单击每个国家/地区的名称以转到不同的网页。

但是我无法从页面中找到任何内容,通过 id、类、名称、xpath 进行搜索,也无法找到或切换到任何 iframe。

起初我试图通过复制 xpath 来找到 Currency 下拉列表:

!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")

然后我发现问题可以链接到iframeshere,但是尝试并没有找到iframes:

wd.find_elements_by_tag_name("iframe")

如何正确访问本页内容?

【问题讨论】:

    标签: python selenium selenium-webdriver iframe selenium-chromedriver


    【解决方案1】:

    当我导航到您的 URL 时,我没有得到您期望的页面。我被重定向到“接受条款和条件”页面:

    您将通过 selenium 获得此功能,因为当您启动 selenium 时,它每次都会创建一个新的用户配置文件。这意味着您的设置、cookie 和缓存不会被使用。

    您可以通过启动新的隐身会话来模拟此行为。

    在您找到任何对象之前,您需要在 cookie 消息上按接受并接受条款和条件页面。

    我还建议您在页面中添加一些同步。你可以阅读 selenium 的等待策略here

    那么,您对脚本中的 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)
    

    因为我只是打印货币输出,所以我得到:

    美元 欧元 当地英镑 日元 加元 瑞士法郎 港元 澳元 MXN

    最后,当您完成框架并返回“主”页面/框架时:

    wd.switch_to_default_content()
    

    【讨论】:

    • 非常感谢!但是,当我尝试执行您的脚本时,我得到:“ElementClickInterceptedException:消息:元素点击被拦截:元素在点 (295, 654) 处不可点击(会话信息:无头 chrome=85.0.4183.83)”在 cookieAccept.click( )
    • 编辑:通过增加等待时间解决了上述问题
    【解决方案2】:

    我有一些建议给你

    • 首先,我真的建议您从 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)
      

    【讨论】:

    • 感谢您的回复!事实上,我忘记了我的问题中的 wd.get 行,但我当然使用了它。但是,您是否真的尝试过您显示的代码?我当然尝试过同样的线路,但它失败了
    猜你喜欢
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    相关资源
    最近更新 更多