簡體   English   中英

如何在 Glassdoor 上使用 Selenium 和 Python 訪問 iframe

[英]How to access the iframe using Selenium and Python on Glassdoor

我正在嘗試使用 EasyApply 按鈕在 Glassdoor 上自動化應用程序。 現在在識別 EasyApply 按鈕並成功單擊它之后,我需要切換到 Frame 以訪問表單的 HTML 內容,以便能夠發送我的表單詳細信息。

我用了:

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

執行切換但仍然無法訪問框架的 html 內容。

這是執行此操作的塊:

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')]")))
  • 使用名稱屬性:

    • 使用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

該頁面中有 2 個 iframe,第二個 iframe 包含另一個(嵌套)iframe。 以下代碼將解決您的問題(設置為linux,但您可以弄清楚 - 注意導入,以及獲取url后的代碼):

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time as t

chrome_options = Options()
chrome_options.add_argument("--no-sandbox")

webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)

url = 'https://www.glassdoor.com/Job/nigeria-data-science-jobs-SRCH_IL.0,7_IN177_KO8,20.htm?clickSource=searchBox'
browser.get(url)

button = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, '//button[@data-test="applyButton"]')))
button.click() 
iframes = WebDriverWait(browser, 20).until(EC.presence_of_all_elements_located((By.TAG_NAME, "iframe")))
print(len(iframes))
for iframe in iframes:
    print(iframe.get_attribute('id'))
browser.switch_to.frame(iframes[1])
t.sleep(2)
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//*[@title='Job application form']")))
t.sleep(2)
applicant_name = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, 'input-applicant.name')))
applicant_name.click()
applicant_name.send_keys('hello dolly')

請讓我知道這對你有沒有用。

更新:網站似乎從昨天開始改變了結構,現在每頁有 11/13 個 iframe。 此外,如果從主欄中選擇的作業不提供 Easy Apply,則會出現錯誤。 更新后的代碼(如下)是從頁面中選擇 Easy Apply 作業,遍歷每個作業,單擊 easyapply 按鈕,select 正確的 iframe/嵌套 iframe,並對該表單進行處理。 它目前有效,直到該網站再次更改其格式:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time as t

chrome_options = Options()
chrome_options.add_argument("--no-sandbox")

webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)

url = 'https://www.glassdoor.com/Job/nigeria-data-science-jobs-SRCH_IL.0,7_IN177_KO8,20.htm?clickSource=searchBox'
browser.get(url)

jobs = WebDriverWait(browser, 20).until(EC.presence_of_all_elements_located((By.XPATH, "//li[@data-is-easy-apply='true']")))

while True:
    for job in jobs:
        try:
            print(job.text)
            job.click()
            try:
                WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@alt='Close']"))).click()
            except Exception as e:
                print('no request to login')

            t.sleep(2)
            button = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, '//button[@data-test="applyButton"]')))
            button.click() 
            WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//*[@title='Job application form container']")))
            t.sleep(2)
            WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//*[@title='Job application form']")))
            t.sleep(2)
            applicant_name = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, 'input-applicant.name')))
            applicant_name.click()
            applicant_name.send_keys('hello dolly') 
            t.sleep(2)
            WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@id='form-action-cancel']"))).click()
            t.sleep(2) 
            browser.switch_to.default_content()


            t.sleep(2)
        except Exception as e:
            print(e)
            break

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM