Python,Selenium:“元素不再附加到DOM" [英] Python, Selenium : 'Element is no longer attached to the DOM'

查看:294
本文介绍了Python,Selenium:“元素不再附加到DOM"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在抓取一个网站, www.lipperleaders.com .我想提取新加坡的资金明细.我已经成功实现了下拉选择,并提取了提交选项后出现的第一页的内容.但是,当我尝试转到下一页(通过使代码单击下一步按钮)时,出现错误'Element is no longer attached to the DOM'.

I am scraping a website, www.lipperleaders.com. I want to extract the funds detail of Singapore. I have successfully implemented drop-down selection and extracted the content of the first page appearing after submission of the options. But when I try to go to next pages (by making the code to click next button) I am getting error 'Element is no longer attached to the DOM'.

我的代码大约有100行,但是我可以大致了解我的代码的执行流程:

My code is about 100 lines but I can give a general idea of the flow of execution of my code:

...                    # creating driver object and all the imports
def main():
    ...
    result = find_elements_by_tag_name('span')  
    ...
    driver.find_element_by_id("ctl00_ContentPlaceHolder1_ucDataPager_btnNext").click()
    main()
main()

此代码在第一页上工作正常,但是在单击下一个按钮后再次调用main()时.在使用这种递归方法之前,我还尝试将其放入循环中,然后再进行同样的错误处理.

This code works fine for the 1st page but when main() is called again after clicking of the next button. Before this recursive method, I also tried putting this inside a loop, then also same error.

如果我编写相同的代码,例如:

And if I write the same code like:

# some code
result = find_elements_by_tag_name('span')  
driver.find_element_by_id("ctl00_ContentPlaceHolder1_ucDataPager_btnNext").click()
# some code
driver.find_element_by_id("ctl00_ContentPlaceHolder1_ucDataPager_btnNext").click()
.
.

此代码在没有错误的情况下都能正常工作,下一页加载并执行此后编写的代码.但是我不能为500个页面编写相同的driver.find_element_by_id().click(),即使我将不得不重复与每个页面相关的其余代码.这就是为什么我要进行循环或递归,但对我不起作用.

This code works fine w/o any error the next page loads and executes the code written after that. But I cannot write the same driver.find_element_by_id().click() for 500 pages, even I will have to repeat the rest of the code associated with each page. That's why I am trying for loop or recursion, but its not working for me.

请让我知道我的方法有什么问题.

Please let me know what is the problem with my approach.

推荐答案

问题是该元素正被某些javascript分离.因此,您应该让驱动程序等待该元素:这是通过设置implicitly_wait来完成的,请参见:

The problem is that the element is being detached by some javascript. So you should make the driver wait for the element: This is done by setting implicitly_wait, see:

from selenium import webdriver

ff = webdriver.Firefox()
ff.implicitly_wait(10) # seconds
...
myDynamicElement = ff.find_element_by_id("myDynamicElement")

来自 http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#implicit-waits

这篇关于Python,Selenium:“元素不再附加到DOM"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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