0

My code goes into a webpage, and can click on each row of the page.

I want to ultimately, open each link of each row as a new page, so I can save the URL of each page.

I cannot get the URL without Seleniums .click() (i believe), so I resorted to .click, and then get the currentURL.

This code works only for the first row, as the webpage exits the original URL.

Is there a more efficient way to print the URLS of each row?

from selenium import webdriver
import time
sam=[]
import pandas as pd
driver = webdriver.Chrome()
for x in range (1,8):
    driver.get(f'https://www.abstractsonline.com/pp8/#!/9325/presentations/endometrial/{x}')
    time.sleep(3)
    page_source = driver.page_source
    eachrow=driver.find_elements_by_xpath('//*[@id="results"]/li')
    for item in eachrow:
        oth = item.find_element_by_xpath(".//h1[@class='name']")
        oth.click()
        print(driver.current_url)

2
  • Are you trying to click on an <h1> tag? If you have an <a> tag, you can fetch the href attribute from it without clicking on it. Commented Mar 18, 2021 at 6:17
  • Yes, how do i do this? Commented Mar 18, 2021 at 6:28

1 Answer 1

1
<h1 class="name" data-id="696" data-key="">
  <span class="title color-primary">
    <i class="icon-caret-right"></i> 
    <span class="bodyTitle">Panel Discussion</span>
  </span>
</h1>

Each of your elements has a data-id grab all of them and just append url

https://www.abstractsonline.com/pp8/#!/9325/presentation/696

https://www.abstractsonline.com/pp8/#!/9325/presentation/{}

You can then grab them by

eachrow=["https://www.abstractsonline.com/pp8/#!/9325/presentation/"+x.get_attribute('data-id') for x in driver.find_elements_by_xpath('//*[@id="results"]/li//h1[@class='name']')]

for row in eachrow:
    driver.get(row)
Sign up to request clarification or add additional context in comments.

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.