【发布时间】: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
但我得到了第一个实际链接,但代码停止了,
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,取第二个项目并获得下一个链接, 直到循环结束。
【问题讨论】:
-
链接是在新窗口还是 iframe 中打开?
-
@Danny 链接在同一窗口中打开
标签: python selenium selenium-webdriver