2

I want to select an option that displays only after you've clicked on the dropdown (see attached image). I have been able to click on the dropdown to get the list, but have not been able to figure out how to click on an option, say option 1, 'Last Day' after the list comes into picture.

enter image description here

Here's what I have so far:

from selenium import webdriver

binary = FirefoxBinary('C:\\Program Files\\Firefox Developer Edition\\firefox.exe')
cap = DesiredCapabilities().FIREFOX
cap["marionette"] = True

url='https://www.glassdoor.com/Job/jobs.htm?suggestCount=0&suggestChosen=false&clickSource=searchBtn&typedKeyword=data+sc&sc.keyword=data+scientist&locT=C&locId=1154532&jobType='    
driver = webdriver.Firefox(firefox_binary=binary, capabilities=cap, executable_path=GeckoDriverManager().install())
driver.get(url=url)
driver.implicitly_wait(10)
driver.maximize_window()

# clicking on dropdown
d = driver.find_element_by_id('filter_fromAge')
d.click()

I also tried using the following code (found on another SO answer) but it did not work either:

element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"ul#css-1dv4b0s ew8xong0")))

I'm new to web scraping and not really familiar with XPATHs and how to deal with actions. Help appreciated!

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Vishesh Shrivastav
  • 2,079
  • 2
  • 16
  • 34
  • 1
    See solution for your question in Answer section and For your xpath below could be good starting pint https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/ https://selenium-python.readthedocs.io/locating-elements.html https://www.guru99.com/xpath-selenium.html – rahul rai Sep 01 '20 at 05:31
  • Try using to click. – Arundeep Chohan Sep 01 '20 at 06:50

3 Answers3

2

You can use Java script to click on your element. As element is present in DOM but its only appears when click on drop down, so normal click method mau work or may not. But with JS it will always click. Can use below code:

day = driver.find_element_by_xpath("//span[contains(text(),'Last Day')]") #Identify your element
driver.execute_script("arguments[0].click();", day) # CLick it with help of JS

out Put:

enter image description here

rahul rai
  • 2,260
  • 1
  • 7
  • 17
2

You can try the following approach:

driver.get('https://www.glassdoor.com/Job/boston-data-scientist-jobs-SRCH_IL.0,6_IC1154532_KO7,21.htm')
driver.maximize_window()

expand_element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, 'filter_fromAge')))
expand_element.click()

target_text = 'Last 3 Days'
target_element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//ul[@class="css-1dv4b0s ew8xong0"]/li/span[text()="{}"]'.format(target_text))))
target_element.click()
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//div[@id="filter_fromAge"]/span[text()="{}"]'.format(target_text))))
frianH
  • 7,295
  • 6
  • 20
  • 45
-1

To select the option with text as Last Day that displays only after clicking on the dropdown you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:

  • Using XPATH:

    driver.get('https://www.glassdoor.com/Job/jobs.htm?suggestCount=0&suggestChosen=false&clickSource=searchBtn&typedKeyword=data+sc&sc.keyword=data+scientist&locT=C&locId=1154532&jobType=')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#filter_fromAge>span"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='PrimaryDropdown']/ul//li//span[@class='label' and contains(., 'Last Day')]"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

LastDay

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352