Issue
I have written this selenium program in python:
from selenium import webdriver
i=0
driver = webdriver.Chrome(executable_path="/usr/local/bin/chromedriver")
driver.get("https://problogger.com/jobs/")
elems = driver.find_elements_by_tag_name('a')
for postings in elems:
href = postings.get_attribute('href')
i = i + 1
if i > 3:
break
else:
if '/job/' in href:
print(href)
This program is not printing any of the job links if I use 'if' logic where 'i' is greater than 3. If I remove that logic, it's printing all the job links, although I only want the top 3 ones. Please tell me how to fix it?
Solution
As mentioned in comments, not all the a
elements are containing job
in their href
attributes.
To print the first 3 jobs links you can do this:
from selenium import webdriver
driver = webdriver.Chrome(executable_path="/usr/local/bin/chromedriver")
driver.get("https://problogger.com/jobs/")
elems = driver.find_elements_by_xpath('//a[contains(@href,"/job/")]')
for posting in islice(elems, 3):
href = posting.get_attribute("href")
print(href)
To make this working don't forget to add appropriate imports.
This should work:
from itertools import islice
Answered By - Prophet
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.