[英]Python, Selenium, BS4 - Navigating to the next Page
Part of my HTML looks like below: 我的HTML的一部分如下所示:
<div id="qryNav">
<form method="post" action="OffQryRedirector.jsp" id="form1" name="form1">
<input type="hidden" name="NextPage" value="7" />
<input type="submit" name="Action" id="oq-nav-begin" value="<<" />
<input type="submit" name="Action" id="oq-nav-prv" value="<" />
<span class="oq-nav-btwn">Page 1 of 4</span>
<input type="submit" name="Action" id="oq-nav-nxt" value=">" />
<input type="submit" name="Action" id="oq-nav-end" value=">>" />
</form>
<a href="OffQryForm.jsp" class="qryNav"><span>Start a New Search</span></a>
<!--<a href="javascript:history.back()" class="qryNav"><span>Modify Your Search</span> </a>-->
</div>
I am trying to identify the Number of pages and then move to the next page. 我正在尝试确定页数,然后移至下一页。 My code looks like below -
我的代码如下所示-
html = driver.page_source
soup = BeautifulSoup(html)
pages = soup.find_all('span', {'class': 'oq-nav-btwn'})[0].text.encode('ascii', 'ignore').strip().upper()
loc_of = pages.find('OF')
num_pages = int(pages[loc_of+2:].strip())
>>> print num_pages
4
span = soup.find_all('span', {'class': 'oq-nav-btwn'})
elem2 = span[0].find_next_sibling()
elem2.find_element_by_id("oq-nav-nxt")
Post this i am trying to run a loop for each of the 4 Pages - 1.. 4. However when i use 发布此信息,我尝试为4页中的每一个运行循环-1.。4.但是,当我使用
elem2.find_element_by_id("oq-nav-nxt").click()
I get the standard selenium.common.exceptions.StaleElementReferenceException: Message: u'stale element reference: element is not attached to the page document\\n (Session info: chrome=34.0.1847.131)\\n (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 x86_64)' 我得到了标准的selenium.common.exceptions.StaleElementReferenceException:消息:u'stale元素引用:元素未附加到页面文档\\ n(会话信息:chrome = 34.0.1847.131)\\ n(驱动程序信息:chromedriver = 2.9。 248315,platform = Windows NT 6.1 x86_64)'
The element is visible. 该元素是可见的。 I dont think that the try.. catch... wait.. is the solution for this.. (I may be wrong here.)
我不认为try..catch ... wait ..是解决此问题的方法..(我在这里可能错了。)
I also tried to do the same with the below code - 我也尝试使用下面的代码做同样的事情-
span = soup.find_all('span', {'class': 'oq-nav-btwn'})
elem2 = span[0].find_next_sibling()
>>> print elem2
<input id="oq-nav-nxt" name="Action" type="submit" value=">">
<input id="oq-nav-end" name="Action" type="submit" value=">>">
</input></input>
But i am unable to navigate the elem2 value above and then click on the "oq-nav-nxt" button. 但是我无法浏览上面的elem2值,然后单击“ oq-nav-nxt”按钮。
Your help on this is appreciated. 感谢您的帮助。
You don't need to use BeautifulSoup
here. 您无需在此处使用
BeautifulSoup
。 Selenium
is pretty powerful in terms of locating elements . Selenium
在元素定位方面非常强大。
One option is to keep finding the next page link by id until it is not found: 一种选择是继续通过id查找下一页链接,直到找不到为止:
while True:
try:
next_button = driver.find_element_by_id('oq-nav-nxt')
except NoSuchElementException:
break
next_button.click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.