Home > Software engineering >  Loop through all clickable links selenium Python
Loop through all clickable links selenium Python

Time:09-23

I'm trying to click each job listing by looping through each one. How can I get to a previous state after I click the link and then click the next link? I tried driver.back(), here I tried driver.get(url) but nothing works. Any help would be appreciated.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.common.exceptions import NoSuchElementException
import csv
import re
import requests
from bs4 import BeautifulSoup

driver_service = Service(executable_path="C:\Program Files (x86)\chromedriver.exe")
driver = webdriver.Chrome(service=driver_service)
driver.maximize_window()  # load web driver
wait = WebDriverWait(driver, 5)
driver.get('https://www.seek.com.au/data-jobs-in-information-communication-technology/in-All-Perth-WA')

looking_job = driver.find_elements(By.XPATH, "//a[@data-automation='jobTitle']")
e = []
count = 1
for xx in looking_job:
    get_url = ""
    get_url  = driver.current_url

    sleep(3)
    second = xx.find_element(By.XPATH, "(//a[@data-automation='jobTitle'])[{}]".format(count)).click()
    sleep(3)
    count  = 1
    driver.get(get_url)
    sleep(1)

CodePudding user response:

looking_job = [x.get_attribute('href') for x in driver.find_elements(By.XPATH, "//a[@data-automation='jobTitle']")]
for job in looking_job:
    driver.get(job)

Just grab all the links and driver.get to them.

  • Related