陈旧元素异常python [英] Stale element exception python

查看:121
本文介绍了陈旧元素异常python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码检索链接:

I am retrieving my link using the following code:

os.environ['MOZ_HEADLESS'] = '1'
binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe', log_file=sys.stdout)
self.driver = webdriver.Firefox(firefox_binary=binary, executable_path='C:/chromedriver/geckodriver')

self.driver.get(link)

接下来,我打电话给

xpath=".//a[@class='tileLink']"
ignored_exceptions = (NoSuchElementException, StaleElementReferenceException,)

your_element = WebDriverWait(self.driver, 30, ignored_exceptions=ignored_exceptions).until(
                expected_conditions.presence_of_element_located((By.XPATH, xpath)))

然后

links = self.driver.find_elements_by_xpath(".//a[@class='tileLink']")
for link in links:
     href_ = link.get_attribute("href") # <<-- Error ehre

link.get_attribute(attribute)引发陈旧元素异常.

现在,考虑到WebDriverWait,我以为我会避免此问题,但仍然存在.

Now, given the WebDriverWait I thought I would avoid this issue, yet it persists.

我很想在加载页面源代码后将其投入,然后将其扔到lxml中以完全避免此问题.

I am tempted to take the page source, once it has loaded, and throw it into lxml to avoid this issue completely.

在建立links与遍历链接之间经过的时间最多为一秒钟.

The time the passes between establishing links and iterating over the links is a second at most.

还有没有其他人遇到过这样的问题,并找到了解决方案?

Has anyone else experienced an issue like this, and found a solution?

任何指导表示赞赏.

推荐答案

如果我已理解您的问题,则需要从用 xpath //a[@class='tileLink']标识的所有元素中获取href属性. .为此,您可以使用以下代码块:

If I have understood your question you need to get the href attribute from all the elements identified with the xpath //a[@class='tileLink']. To achieve that you can use the following code block :

# xpath=".//a[@class='tileLink']"
# ignored_exceptions = (NoSuchElementException, StaleElementReferenceException,)
# links = WebDriverWait(self.driver, 30, ignored_exceptions=ignored_exceptions).until(expected_conditions.presence_of_element_located((By.XPATH, xpath)))
links = WebDriverWait(self.driver, 30).until(expected_conditions.visibility_of_all_elements_located((By.XPATH, "//a[@class='tileLink']")))
for link in links:
     print(link.get_attribute("href"))

这篇关于陈旧元素异常python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆