Home > Software design >  conditional python selenium to skip extracted clickable links
conditional python selenium to skip extracted clickable links

Time:09-23

I linked two pictures below. Looking within both a tag, I want to extract only the 'quick apply' job postings which are defined with the target='self' compared to the external apply which is defined by target='_blank'. I want to put a conditional to exlcude all the _blank profiles. I'm confused but I assume it would follow some logic like:

quick_apply = driver.find_element(By.XPATH, "//a[@data-automation='job-detail-apply']")
internal = driver.find_element(By.XPATH, "//a[@target='_self']")
if internal in quick_apply:
     quick_apply.click()
else:
     pass



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

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 = [x.get_attribute('href') for x in driver.find_elements(By.XPATH, "//a[@data-automation='jobTitle']")]
for job in looking_job:
    driver.get(job)
    quick_apply = driver.find_element(By.XPATH, "//a[@data-automation='job-detail-apply']").click()

enter image description here

enter image description here

CodePudding user response:

You can merged two conditions in single xpath. 1.Use WebDriverWait() and wait for element to be clickable.

2.Use try..except block to check if element there then click.

3.There are pages where you found two similar elements, where last element is clickable, that's why you need last() option to identify the element.

code.

driver.get('https://www.seek.com.au/data-jobs-in-information-communication-technology/in-All-Perth-WA')
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)
    try:
      quick_apply = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"(//a[@data-automation='job-detail-apply' and @target='_self'])[last()]")))
      quick_apply.click()
    except:
      print("No records found")
      pass
  • Related