如何在 python 中使用 selenium 单击网站上的继续按钮?

How to click the Continue button on website using selenium in python?

我正在尝试编写能够自动申请 indeed.com 职位空缺的代码。我已经成功进入了最后阶段,但是,最后一次点击申请表给我带来了很多麻烦。请参考以下页面

登录到我的个人资料后,我会转到相关搜索页面,单击我感兴趣的列表,然后在最后一页(如上所示)我试图单击 continue使用 xpath 的按钮如下:

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()
driver.find_element_by_xpath('//*[@id="form-action-continue"]')

但是,这给了我一个错误:

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="form-action-continue"]"}

在浏览了网上的一些建议后,我什至尝试了以下方法:

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()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="form-action-continue"]')))

但是这给了我一个超时错误

TimeoutException: Message:

将不胜感激。

一旦我遇到类似的问题和标准 time.sleep() 直到出现表单(或继续按钮)帮助了我。尝试用它代替 WebDriverWait,也许它会起作用。

看来,在该表格上有多个 iframe,这就是您出错的原因。

你需要得到第一个 iframe,切换到它,在第一个里面得到第二个 iframe,切换到它,然后你才能得到 continue 按钮。

像这样应该可以解决问题:

    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()