Part of my HTML looks like below:
<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
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)'
The element is visible. I dont think that the try.. catch... wait.. is the solution for this.. (I may be wrong here.)
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.
Your help on this is appreciated.
You don't need to use BeautifulSoup
here. Selenium
is pretty powerful in terms of locating elements .
One option is to keep finding the next page link by id until it is not found:
while True:
try:
next_button = driver.find_element_by_id('oq-nav-nxt')
except NoSuchElementException:
break
next_button.click()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.