Reputation: 1832
I have a page with 25 Companies located in same class. Here is the link for the website - This is the HTML code:
<section class="rslwrp">
<section class="jbbg">..</section>
<section class="jbbg">..</section>
<section class="jbbg">..</section>
<section class="jbbg">..</section>
<section class="jbbg">..</section>
<section class="jbbg">..</section>
******and so on******
I am not sure, how I make selenium click one class and then browser.back() then move to second then again browser.back()
and then to third and so on. For n number of times.
I am using,
browser.find_element_by_xpath('//section[@class="jbbg"]/section[2]/section[1]/aside[1]/p[1]/span/a').click()
Could someone please advise. Thanks in advance for your help.
Upvotes: 1
Views: 1082
Reputation: 21
As the previous poster mentioned, you should iterate through each of them for clicking. However, inside the loop, you have to identify the application back button or browser back button to go back to previous page.
But, note once you gone back to the previous page, the element you grabbed with find_elements may not be valid or stale due to html reload. So, you might want to re locate the same element again inside the loop for the program to work
Upvotes: 0
Reputation: 474001
Loop over the results of find_elements_by_xpath()
, get all links and get()
them one by one:
links = [link.get_attribute('href') for link in browser.find_elements_by_xpath('//span[@class="jcn"]/a')]
for link in links:
browser.get(link)
Upvotes: 4
Reputation: 9029
Simplest thing to do is to use find_elements_by_xpath
, then iterate through the list:
linksCount = len(browser.find_elements_by_xpath('//section[@class="jbbg"]/section[2]/section[1]/aside[1]/p[1]/span/a'))
for x in range(linksCount)
browser.find_element_by_xpath('(//section[@class="jbbg"]/section[2]/section[1]/aside[1]/p[1]/span/a)[' + x + ']')
Upvotes: 0