【问题标题】:How to Get the Current URL of an Item in a Loop Using Selenium如何使用 Selenium 在循环中获取项目的当前 URL
【发布时间】:2017-11-29 11:06:20
【问题描述】:

我正在尝试获取已在循环中的项目的当前 url

def get_financial_info(self):
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--window-size=1920x1080")
    driver = webdriver.Chrome(executable_path='/path/chromedriver')

    driver.get("https://www.financialjuice.com")

    try:
        WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='trendWrap']")))
    except TimeoutException:
        driver.quit()

    category_url = [a.get_attribute("href") for a in
                    driver.find_elements_by_xpath("//ul[@class='nav navbar-nav']/li[@class='text-uppercase']/a[@href]")]

    for record in category_url:
        driver.get(record)
        item = {}
        url_element = webdriver.find_elements_by_xpath("//p[@class='headline-title']/a[@href]")

        for links in url_element:
            driver.get(links.get_attribute("href"))
            print driver.current_url

但我得到了第一个实际链接,但代码停止了,

http://www.zerohedge.com/news/2017-08-26/brief-history-tail-risk-ltcm-abx-cds-vix?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+zerohedge%2Ffeed+%28zero+hedge+-+on+a+long+enough+timeline%2C+the+survival+rate+for+everyone+drops+to+zero%29

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: headless chrome=62.0.3202.94)
  (Driver info: chromedriver=2.33.506092 (733a02544d189eeb751fe0d7ddca79a0ee28cce4),platform=Linux 4.4.0-101-generic x86_64)

我试着研究发生了什么,我意识到,网络驱动程序打开了第一个类别,选择第一个项目并获得了实际的链接,它停止了而不是回到上一个 url,取第二个项目并获得下一个链接, 直到循环结束。

【问题讨论】:

标签: python selenium selenium-webdriver


【解决方案1】:

您应该为内部for 循环实现与外部循环相同的方法。替换

url_element = webdriver.find_elements_by_xpath("//p[@class='headline-title']/a[@href]")

for links in url_element:
    driver.get(links.get_attribute("href"))
    print driver.current_url

url_element = [a.get_attribute('href') for a in webdriver.find_elements_by_xpath("//p[@class='headline-title']/a")]
for link in url_element:
    driver.get(link)
    print driver.current_url

【讨论】:

  • 这有点效果,但总是超时,我该怎么做才能避免这种情况
  • @molecules 你能解释一下超时是什么意思吗?它在哪里超时,异常的相关部分是什么?
  • 当代码显示为 10 个项目时,我仍然得到不受支持的协议和 @mrfreester,我得到超时错误
  • 分享确切的异常日志
  • 根据提供的链接,您的代码包含更多行,因此有更多错误原因。当前 SO 票证中提供的代码不包含这些行,因此我的代码只能解决当前问题...我认为您应该针对新问题打开新票证
猜你喜欢
  • 1970-01-01
  • 2020-11-23
  • 2017-02-13
  • 2013-04-05
  • 1970-01-01
  • 2012-07-17
  • 1970-01-01
  • 2020-12-18
相关资源
最近更新 更多