如何使用python selenium关闭poverform?

2024-06-11 17:04:31 发布

您现在位置:Python中文网/ 问答频道 /正文

从是的我试着自动得到职位,雇主和描述。在

它工作得很好,直到循环进入第二页。在

弹出一个弹出窗体,在那里我应该按“Nein,danke.”按钮继续。当这个改变出现时,循环就停止了。在

我的代码如下所示,在第二页之前都可以正常工作:

# Import the packages
from selenium import webdriver
from time import sleep
import nltk
from nltk.tokenize import word_tokenize
import numpy as np
import pandas as pd
import gensim

# Start Webscraping
driver = webdriver.Safari()
driver.maximize_window()

# List with indeed URLs to scrape through
indeed_url_list = ['https://de.indeed.com/Jobs?q=data&l=&sort=date',
                   'https://de.indeed.com/jobs?q=Data&sort=date&start=10',
                   'https://de.indeed.com/jobs?q=Data&sort=date&start=20',
                   'https://de.indeed.com/jobs?q=Data&sort=date&start=30',
                   'https://de.indeed.com/jobs?q=Data&sort=date&start=40'
                   ]

# Empty lists that will be filled
indeed_job_links = []         # list with links to scrape through
indeed_job_titles = []        # list with job titles
indeed_job_employers = []     # list with job employers
indeed_job_descriptions = []  # list with job descriptions

# for loop for scraping
for indeed_page in indeed_url_list:    
    driver.get(indeed_page)    
    links = driver.find_elements_by_xpath('//div[@class="jobsearch-SerpJobCard row result clickcard" or @class="jobsearch-SerpJobCard row sjlast result clickcard" or @class="jobsearch-SerpJobCard row result clickcard vjs-highlight" or @class="jobsearch-SerpJobCard lastRow row result clickcard" or @class="jobsearch-SerpJobCard row result clickcard vjs-highlight"]/*/a')     

    # get job link to list
    for i in list(links):
        indeed_job_links.append(i.get_attribute('href'))

    # scrape through the job descriptions
    for link in links:
        # open the link
        link.click()
        sleep(0.6)
        # get job title to list
        indeed_title = driver.find_element_by_xpath('//div[@id="vjs-jobtitle"]').text
        indeed_job_titles.append(indeed_title)
        # get job employer to list
        indeed_employer = driver.find_element_by_xpath('//span[@id="vjs-cn"]').text
        indeed_job_employers.append(indeed_employer)
        # get job description to list
        indeed_description = ' '.join(word_tokenize(driver.find_element_by_xpath('//div[@id="vjs-desc"]').text))
        indeed_job_descriptions.append(indeed_description)

我真的不知道该怎么办。有人有主意吗?非常感谢你。在


Tags: tohttpsimportcomforgetdatedriver
2条回答

如果这是真正的警报,这应该对你有用。作为参考,here is the relevant section of the Selenium documentation

alertObj = driver.switch_to.alert
alertObj.accept()
alertObj.dismiss()

在测试我们的应用程序时,我经常处理警报。我发现他们可能是相当不可靠的网页需要多长时间来呈现他们。这是我的标准实现。在

^{pr2}$

我看了看你提到的那个流行音乐是的. 它是一个对话框,而不是一个浏览器警报,因此之前的答案(您切换到警报)将不起作用。在

driver.switch_to.alert 

只在JavaScript警报的情况下有效。在

你看到了什么是的是一个HTML对话框,它应该像其他页面元素一样被处理。在

第一次更改页面时,您知道对话框将显示,所以请等待它并关闭它。我不确定它是否会出现在你改变N页数后。但如果是这样的话,你可以考虑给它发一封电子邮件来阻止它突然出现,而不是关闭它。或者每次更改页面时,都可以检查是否显示对话框并将其关闭-这是不太理想的。在

下面是您的代码,在支持关闭对话框后稍作整理:

^{pr2}$

相关问题 更多 >