python - 如何在 Glassdoor 上使用 Selenium 和 Python 访问 iframe

标签 python selenium xpath iframe css-selectors

我正在尝试使用 EasyApply 按钮在 Glassdoor 上自动化申请流程。现在,在识别 EasyApply 按钮并成功单击它之后,我需要切换到框架,以便访问表单的 HTML 内容,以便能够发送我的表单详细信息。

我用过:

wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "#indeedapply-modal-preload-1658752913396-iframe")))

执行切换,但仍然无法访问框架的 html 内容。

这是执行此操作的 block :

from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
#if it has easy-apply, then perform application

if len(driver.find_elements_by_xpath('//*[@id="JDCol"]/div/article/div/div[1]/div/div/div[1]/div[3]/div[2]/div/div[1]/div[1]/button')) > 0:
    driver.find_element_by_xpath('//*[@id="JDCol"]/div/article/div/div[1]/div/div/div[1]/div[3]/div[2]/div/div[1]/div[1]/button').click()
    wait = WebDriverWait(driver, 50)
    time.sleep(5)
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "#indeedapply-modal-preload-1658752913396-iframe")))
    name = driver.find_element(By.CSS_SELECTOR, '#input-applicant.name')
    name.send_keys('Oluyele Anthony')
elif len(driver.find_elements_by_xpath('//*[@id="JDCol"]/div/article/div/div[1]/div/div/div[1]/div[3]/div[2]/div/div[1]/div[1]/a')) > 0:
    driver.find_element_by_xpath('//*[@id="JDCol"]/div/article/div/div[1]/div/div/div[1]/div[3]/div[2]/div/div[1]/div[1]/a').click()

这是框架的 HTML 内容

<iframe name="indeedapply-modal-preload-1659146630884-iframe" id="indeedapply-modal-preload-1659146630884-iframe" scrolling="no" frameborder="0" title="Job application form container" src="https://apply.indeed.com/indeedapply/xpc?v=5#%7B%22cn%22:%224AaHlXdnW4%22,%22ppu%22:%22https://www.glassdoor.com/robots.txt%22,%22lpu%22:%22https://apply.indeed.com/robots.txt%22,%22setupms%22:1659146630959,%22preload%22:true,%22iaUid%22:%221g96dgr7uii3h800%22,%22parentURL%22:%22https://www.glassdoor.com/Job/nigeria-data-science-jobs-SRCH_IL.0,7_IN177_KO8,20.htm?clickSource=searchBox%22%7D" style="border: 0px; vertical-align: bottom; width: 100%; height: 100%;"></iframe>

显然,运行单元后:

TimeoutException: Message:

发生错误,表明帧没有被切换到。

最佳答案

id属性值的中间部分,即 1658752913396是动态生成的,并且迟早会发生变化。当您下次重新访问应用程序时,甚至在下次应用程序启动时,它们可能会发生变化。因此不能在定位器中使用。


解决方案

切换到 <iframe> 您可以使用以下任一 Locator Strategies :

  • 使用id属性:

    • 使用CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='Job application form container'][id^='indeedapply-modal-preload']")))
      
    • 使用XPATH:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[title='Job application form container' and starts-with(@id, 'indeedapply-modal-preload')]")))
      
  • 使用name属性:

    • 使用CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='Job application form container'][name^='indeedapply-modal-preload']")))
      
    • 使用XPATH:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[title='Job application form container' and starts-with(@name, 'indeedapply-modal-preload')]")))
      
  • 注意:您必须添加以下导入:

     from selenium.webdriver.support.ui import WebDriverWait
     from selenium.webdriver.common.by import By
     from selenium.webdriver.support import expected_conditions as EC
    

关于python - 如何在 Glassdoor 上使用 Selenium 和 Python 访问 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73172396/

相关文章:

python - 有兴趣在维基百科 xml 转储中仅搜索与医学相关的术语

java - 如何在方法中使用 doGet 响应?

java - 如何在 Java 中使用 XPath 读取 XML

css - Webdriver.io 如何根据页面上类似元素的数量选择和更改元素

sql-server - 将 XML 从一种格式转换为另一种格式

python - 如何从 C 到 Python 编写这个 for 循环?

python - Keras : My model trains without any given labels. 这怎么可能?

python - 使用 beautifulSoup4 解析 html 表时出现属性错误

post - 是否可以在 Selenium 中捕获 POST 数据?

java - Selenium 测试元素存在失败