1

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.

3 Answers 3

4

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)
Sign up to request clarification or add additional context in comments.

3 Comments

This will likely fail with a staleelementreferenceexception.
@Richard yup, give me a sec.
@Richard check it out - first we get all of the links, then follow them. Think it is an option too.
0

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 + ']')

1 Comment

Thanks for your help, but It shouldnt be right, since it would give an error of "cannot concatenate 'str' and 'int' objects"
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.