Home > OS >  How do you turn a string with url into a url that is clickable using selenium?
How do you turn a string with url into a url that is clickable using selenium?

Time:02-16

url="https://fourminutebooks.com/book-summaries/"
driver.get(url)

fin_list = []
page_tabs = driver.find_elements(By.CSS_SELECTOR, "a[class='post_title w4pl_post_title']")
for i in range(len(page_tabs)):
  page_tabs[i] = page_tabs[i].get_attribute("href")
  fin_list.append(page_tabs[i])

fin_list[0].click()
#html = driver.find_elements(By.CSS_SELECTOR,"header[class='entry-header page-header']")

print(fin_list)

I am trying to create a program that randomly emails me book summaries, and am having difficulty clicking on the link to get the HTML content. I have managed to get all the links, but they are saved as a string and I cannot click on one of the links without getting an error. **note the image below

This is without trying to get the first element.

CodePudding user response:

page_tabs = [x.get_attribute('href') for x in driver.find_elements(By.CSS_SELECTOR, "a[class='post_title w4pl_post_title']")]

for page in page_tabs:
    driver.get(page)

To simply go through all the links you can use the following. I wouldn't recommend clicking through all a tags since you might run into stale elements which you would then need to reobtain elements and so forth.

CodePudding user response:

You can not turn a string to be a web element object.
These are definitely different objects.
As a user you can click a web element on a web page.
Similarly, Selenium can click on a web element. It can not click string or int.
In case you want to collect a list of clickable objects you have to collect a clickable web elements.
In your particular case you can keep a page_tabs list to click on page_tabs[0] element later with

page_tabs[0].click()
  • Related