How to click the Continue button within https://in.indeed.com/ website using selenium in python?

故事扮演 提交于 2021-01-28 12:30:17

问题


I am trying to write a code that is able to auto apply on job openings on indeed.com. I have managed to reach the last stage, however, the final click on the application form is giving me a lot of trouble. Please refer the page as below

Once logged in to my profile, I go to the relevant search page, click on the listing I am interested in and then on the final page (shown above) I am trying to click on the continue button using xpath. For the previous step (also a page on the same form), the form had multiple iframes which I was able to switch successfully. Now for the current page, I am stuck as the click function does not do anything. I have written the following code:

driver.get("https://in.indeed.com/jobs?q=data%20analyst&l=Delhi&vjk=5c0bd416675cf4e5")
driver.find_element_by_xpath('//*[@id="apply-button-container"]/div[1]/span[1]').click()
time.sleep(5)
frame_1 = driver.find_element_by_css_selector('iframe[title="Job application form container"')
driver.switch_to.frame(frame_1)
frame_2 = driver.find_element_by_css_selector('iframe[title="Job application form"]')
driver.switch_to.frame(frame_2)
continue_btn = driver.find_element_by_css_selector('#form-action-continue')
continue_btn.click()
driver.find_element_by_xpath('//*[@id="form-action-continue"]').click() 

I have tried switching the iframes again for this step but nothing happens. Even the .click() function does not do anything.

Will appreciate some help on this.


回答1:


The element Continue is within nested <iframe> elements so you have to:

  • Induce WebDriverWait for the parent frame to be available and switch to it.

  • Induce WebDriverWait for the child frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      driver.get("https://in.indeed.com/jobs?q=data%20analyst&l=Delhi&vjk=5c0bd416675cf4e5")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.indeed-apply-button-label"))).click()
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[id^='indeedapply-modal-preload']")))
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='Job application form']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#form-action-continue"))).click()
      
    • Using XPATH:

      driver.get("https://in.indeed.com/jobs?q=data%20analyst&l=Delhi&vjk=5c0bd416675cf4e5")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='indeed-apply-button-label']"))).click()
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@id, 'indeedapply-modal-preload')]")))
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@title='Job application form']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='form-action-continue']"))).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:


Reference

You can find a couple of relevant discussions in:

  • Ways to deal with #document under iframe
  • Switch to an iframe through Selenium and python
  • selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
  • selenium in python : NoSuchElementException: Message: no such element: Unable to locate element


来源:https://stackoverflow.com/questions/65553387/how-to-click-the-continue-button-within-https-in-indeed-com-website-using-sel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!