TypeError:"FirefoxWebElement"对象在带有Selenium的动态网页上循环浏览网页时不会出现迭代错误 [英] TypeError: 'FirefoxWebElement' object is not iterable error cycling through pages on a dynamic webpage with Selenium
问题描述
这是我要抓取的
然后单击第二个并执行相同的操作:
依次类推,直到第51页.我知道如何使用硒单击第二页:
link ="http://www.nigeriatradehub.gov.ng/Organizations";驱动程序= webdriver.Firefox()driver.get(链接)xpath ='/html/body/form/div [3]/div [4]/div [1]/div/div/div [1]/div/div/div/div/div/div/div [2]/div[2]/span/a [1]'find_element_by_xpath(xpath).click()
但是我不知道如何设置代码,以使其遍历每个页面.首先,我获取xpath的过程是手动的(我继续使用Firefox,检查该项目并将其复制到代码中),所以我不知道如何自动执行该步骤,然后再自动执行以下的.
我尝试在网页html中上一层,选择包含所需元素的页面的整个部分,然后循环浏览它们,但这不起作用,因为它是Firefox Web对象(请参见下文).这是页面源相关部分的快照:
通过像这样调用更高级别的xpath:
path ='//* [@ id ="dnn_ctr454_View_OrganizationsListViewDataPager"]]'driver.find_element_by_xpath(路径)
并尝试查看是否可以循环运行
在driver.find_element_by_xpath(path)中用于我的 :i.click()
我收到以下错误:
任何建议将不胜感激.
此错误消息...
...表示您正在尝试通过site I want to scrape.
I want to scrape all the information in the table on the first page:
then click on the second and do the same:
And so on until the 51st page. I know how to use selenium to click on page two:
link = "http://www.nigeriatradehub.gov.ng/Organizations"
driver = webdriver.Firefox()
driver.get(link)
xpath = '/html/body/form/div[3]/div[4]/div[1]/div/div/div[1]/div/div/div/div/div/div[2]/div[2]/span/a[1]'
find_element_by_xpath(xpath).click()
But I don't know how to set the code up so that it cycles through each page. The process of me getting the xpath is a manual one in the first place (I go on to Firefox, inspect the item and copy it into the code), so I don't know how to automate that step in and of itself and then the following ones.
I tried going a level higher in the webpage html and choosing the entire section of the page with the elements I want, and cycling through them, but that doesn't work because it's a Firefox web object(see below). Here'a a snapshot of the relevant part of the page source:
By calling the xpath of the higher class like so:
path = '//*[@id="dnn_ctr454_View_OrganizationsListViewDataPager"]'
driver.find_element_by_xpath(path)
and trying to see if I can cycle though it:
for i in driver.find_element_by_xpath(path):
i.click()
I get the following error:
Any advice would be greatly appreciated.
This error message...
...implies that you are trying to iterate through a WebElement where as only list objects are iterable.
Solution
Within the for()
loop to create a list to iterate through it's elements, instead of using find_element*
you need to use find_elements*
. So your effective code block will be:
for i in driver.find_elements_by_xpath(path):
i.click()
这篇关于TypeError:"FirefoxWebElement"对象在带有Selenium的动态网页上循环浏览网页时不会出现迭代错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!