试图关闭popover-python-selenium-Glassdoor
在刮玻璃门找工作的时候试图关上一个爆米花[它不时弹出-每次都需要关上它]。。我试过很多东西 试图通过查找“关闭”按钮来关闭它。请帮忙试图关闭popover-python-selenium-Glassdoor,python,selenium,web-scraping,Python,Selenium,Web Scraping,在刮玻璃门找工作的时候试图关上一个爆米花[它不时弹出-每次都需要关上它]。。我试过很多东西 试图通过查找“关闭”按钮来关闭它。请帮忙 driver.find_element_by_class_name("SVG_Inline modal_closeIcon").click() 当机器人无法点击下一家公司时,尝试寻找ElementClickInterceptedException,而其他任何地方都有点击 element = WebDriverWait(driver, 3).until(EC.pr
driver.find_element_by_class_name("SVG_Inline modal_closeIcon").click()
当机器人无法点击下一家公司时,尝试寻找ElementClickInterceptedException,而其他任何地方都有点击
element = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.CLASS_NAME, "SVG_Inline-svg modal_closeIcon-svg")))
element.click()
这是网站:
这是完整的代码:
from selenium.common.exceptions import NoSuchElementException, ElementClickInterceptedException, StaleElementReferenceException
from selenium import webdriver
import time
import pandas as pd
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def get_jobs(keyword, num_jobs, verbose, place):
'''Gathers jobs as a dataframe, scraped from Glassdoor'''
#Initializing the webdriver
options = webdriver.ChromeOptions()
#Uncomment the line below if you'd like to scrape without a new Chrome window every time.
#options.add_argument('headless')
#Change the path to where chromedriver is in your home folder.
#driver = webdriver.Chrome(executable_path="/Users/omersakarya/Documents/GitHub/scraping-glassdoor-selenium/chromedriver", options=options)
driver = webdriver.Chrome()
driver.set_window_size(1120, 1000)
url = "https://www.glassdoor.co.uk/Job/web-developer-jobs-SRCH_KO0,13.htm"
driver.get(url)
jobs = []
time.sleep(3)
driver.find_element_by_id("onetrust-accept-btn-handler").click()
time.sleep(3)
while len(jobs) < num_jobs: #If true, should be still looking for new jobs.
job_buttons = driver.find_elements_by_class_name("jl") #jl for Job Listing. These are the buttons we're going to click.
try:
for job_button in job_buttons:
if len(jobs) >= num_jobs:
break
print("Progress: {}".format("" + str(len(jobs)) + "/" + str(num_jobs)))
job_button.click()
collected_successfully = False
while not collected_successfully:
try:
company_name = driver.find_element_by_xpath('.//div[@class="employerName"]').text
location = driver.find_element_by_xpath('.//div[@class="location"]').text
job_title = driver.find_element_by_xpath('.//div[contains(@class, "title")]').text
job_description = driver.find_element_by_xpath('.//div[@class="jobDescriptionContent desc"]').text
collected_successfully = True
except:
time.sleep(5)
try:
#salary_estimate = driver.find_element_by_xpath('.//span[@class="gray salary"]').text
salary_estimate = driver.find_element_by_xpath('//*[@id="HeroHeaderModule"]/div[3]/div[1]/div[4]/span').text
except NoSuchElementException:
salary_estimate = -1 #You need to set a "not found value. It's important."
try:
rating = driver.find_element_by_xpath('.//span[@class="rating"]').text
except NoSuchElementException:
rating = -1 #You need to set a "not found value. It's important."
#Printing for debugging
if verbose:
print("Job Title: {}".format(job_title))
print("Salary Estimate: {}".format(salary_estimate))
print("Job Description: {}".format(job_description[:500]))
print("Rating: {}".format(rating))
print("Company Name: {}".format(company_name))
print("Location: {}".format(location))
#Going to the Company tab...
#clicking on this:
#<div class="tab" data-tab-type="overview"><span>Company</span></div>
try:
driver.find_element_by_xpath('.//div[@class="tab" and @data-tab-type="overview"]').click()
try:
#<div class="infoEntity">
# <label>Headquarters</label>
# <span class="value">San Francisco, CA</span>
#</div>
headquarters = driver.find_element_by_xpath('.//div[@class="infoEntity"]//label[text()="Headquarters"]//following-sibling::*').text
except NoSuchElementException:
headquarters = -1
try:
size = driver.find_element_by_xpath('.//div[@class="infoEntity"]//label[text()="Size"]//following-sibling::*').text
except NoSuchElementException:
size = -1
try:
founded = driver.find_element_by_xpath('.//div[@class="infoEntity"]//label[text()="Founded"]//following-sibling::*').text
except (NoSuchElementException, StaleElementReferenceException):
founded = -1
try:
type_of_ownership = driver.find_element_by_xpath('.//div[@class="infoEntity"]//label[text()="Type"]//following-sibling::*').text
except NoSuchElementException:
type_of_ownership = -1
try:
industry = driver.find_element_by_xpath('.//div[@class="infoEntity"]//label[text()="Industry"]//following-sibling::*').text
except NoSuchElementException:
industry = -1
try:
sector = driver.find_element_by_xpath('.//div[@class="infoEntity"]//label[text()="Sector"]//following-sibling::*').text
except NoSuchElementException:
sector = -1
try:
revenue = driver.find_element_by_xpath('.//div[@class="infoEntity"]//label[text()="Revenue"]//following-sibling::*').text
except NoSuchElementException:
revenue = -1
try:
competitors = driver.find_element_by_xpath('.//div[@class="infoEntity"]//label[text()="Competitors"]//following-sibling::*').text
except NoSuchElementException:
competitors = -1
except (NoSuchElementException,ElementClickInterceptedException,StaleElementReferenceException): #Rarely, some job postings do not have the "Company" tab.
if NoSuchElementException:
time.sleep(1)
headquarters = -1
size = -1
founded = -1
type_of_ownership = -1
industry = -1
sector = -1
revenue = -1
competitors = -1
else:
driver.find_element_by_class_name("selected").click()
driver.find_element_by_class_name("SVG_Inline modal_closeIcon").click()
element = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.CLASS_NAME, "SVG_Inline-svg modal_closeIcon-svg")))
element.click()
pass
if verbose:
print("Headquarters: {}".format(headquarters))
print("Size: {}".format(size))
print("Founded: {}".format(founded))
print("Type of Ownership: {}".format(type_of_ownership))
print("Industry: {}".format(industry))
print("Sector: {}".format(sector))
print("Revenue: {}".format(revenue))
print("Competitors: {}".format(competitors))
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
jobs.append({"Job Title" : job_title,
"Salary Estimate" : salary_estimate,
"Job Description" : job_description,
"Rating" : rating,
"Company Name" : company_name,
"Location" : location,
"Headquarters" : headquarters,
"Size" : size,
"Founded" : founded,
"Type of ownership" : type_of_ownership,
"Industry" : industry,
"Sector" : sector,
"Revenue" : revenue,
"Competitors" : competitors})
#You might
#time.sleep(0.5)
except (ElementClickInterceptedException, StaleElementReferenceException):
alertObj = driver.switch_to.alert
alertObj.accept()
alertObj.dismiss()
driver.find_element_by_class_name("selected").click()
driver.find_element_by_class_name("SVG_Inline modal_closeIcon").click()
element = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.CLASS_NAME, "SVG_Inline-svg modal_closeIcon-svg")))
element.click()
pass
#add job to jobs
#Clicking on the "next page" button
# try:
# driver.find_element_by_xpath('.//li[@class="page"]//a').click()
# except NoSuchElementException:
# print("Scraping terminated before reaching target number of jobs. Needed {}, got {}.".format(num_jobs, len(jobs)))
# break
# time.sleep(5)
try:
driver.find_element_by_xpath('.//li[@class="next"]//a').click()
except (ElementClickInterceptedException):
#print("Scraping terminated before reaching target number of jobs. Needed {}, got {}.".format(num_jobs, len(jobs)))
driver.find_element_by_class_name("selected").click()
driver.find_element_by_class_name("SVG_Inline modal_closeIcon").click()
element = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.CLASS_NAME, "SVG_Inline-svg modal_closeIcon-svg")))
element.click()
element.text
pass
#break
return pd.DataFrame(jobs) #This line converts the dictionary object into a pandas DataFrame.
df = gs.get_jobs(keyword, num_jobs, False, place)
从selenium.common.exceptions导入NoTouchElementException、元素ClickInterceptedException、StaleElementReferenceException
从selenium导入webdriver
导入时间
作为pd进口熊猫
从selenium.webdriver.common.by导入
从selenium.webdriver.support.ui导入WebDriverWait
从selenium.webdriver.support将预期的_条件导入为EC
def get_作业(关键字、num_作业、详细信息、位置):
“将作业收集为数据框,从Glassdoor中刮取”
#初始化webdriver
options=webdriver.ChromeOptions()
#如果你想在每次都不使用新的Chrome窗口的情况下进行清理,请取消对下面这行的注释。
#选项。添加参数(“无头”)
#更改chromedriver在主文件夹中的路径。
#driver=webdriver.Chrome(可执行文件路径=“/Users/omersakarya/Documents/GitHub/scraping glassdoor selenium/chromedriver”,options=options)
driver=webdriver.Chrome()
驱动程序。设置窗口大小(11201000)
url=”https://www.glassdoor.co.uk/Job/web-developer-jobs-SRCH_KO0,13.htm“
获取驱动程序(url)
工作=[]
时间。睡眠(3)
驱动程序。通过\u id(“onetrust接受btn处理程序”)查找\u元素。\u。单击()
时间。睡眠(3)
而len(jobs)=num_jobs:
打破
打印(“进度:{}”。格式(“+str(len(jobs))+”/“+str(num_jobs)))
作业按钮。单击()
成功收集\u=错误
未成功收集\u时:
尝试:
公司名称=驱动程序。通过xpath('.//div[@class=“employerName”]')查找元素。文本
location=driver。通过xpath('.//div[@class=“location”]')查找元素。text
job_title=driver.find_element_by_xpath('.//div[contains(@class,“title”)]')。text
job_description=driver.find_element_by_xpath('.//div[@class=“jobsdescriptioncontent desc”]')。text
已成功收集\u=真
除:
时间。睡眠(5)
尝试:
#salary_estimate=driver.by_xpath('.//span[@class=“gray salary”]')查找_元素。text
工资估算=驱动程序。通过xpath('/*[@id=“HeroHeaderModule”]/div[3]/div[1]/div[4]/span')查找元素。文本
除无任何例外:
salary_estimate=-1#您需要设置一个“未找到的值。这很重要。”
尝试:
rating=driver。通过xpath('.//span[@class=“rating”]')查找元素。text
除无任何例外:
评级=-1#您需要设置“未找到值。这很重要。”
#用于调试的打印
如果冗长:
打印(“职务:{}”。格式(职务)
打印(“工资估算:{}”。格式(工资估算))
打印(“职务说明:{}”。格式(职务说明[:500]))
打印(“评级:{}”。格式(评级))
打印(“公司名称:{}”。格式(公司名称))
打印(“位置:{}”。格式(位置))
#正在转到“公司”选项卡。。。
#点击此按钮:
#公司
尝试:
驱动程序。通过xpath('.//div[@class=“tab”和@data tab type=“overview”]”查找元素。单击()
尝试:
#
#总部
CA旧金山
#
总部=驱动程序。通过xpath('.//div[@class=“infoEntity”]//标签[text()=“总部”]//以下同级::*')查找元素。text
除无任何例外:
总部=-1
尝试:
size=driver。通过xpath('.//div[@class=“infoEntity”]//label[text()=“size”]//以下同级::*')查找元素。text
除无任何例外:
大小=-1
尝试:
founded=driver。通过xpath('.//div[@class=“infoEntity”]//label[text()=“founded”]//以下同级::*')查找元素。text
除了(NoSuchElementException,StaleElementReferenceException):
成立=-1
尝试:
type_of_ownership=driver.by_xpath('.//div[@class=“infoEntity”]//label[text()=“type”]//以下同级::*')查找_元素。text
除无任何例外:
所有权的类型=-1
尝试:
行业=驱动程序。通过xpath('.//div[@class=“infoEntity”]//标签[text()=“行业”]//以下同级::*')查找元素。text
除无任何例外:
行业=-1
尝试:
sector=driver。通过xpath('.//div[@class=“infoEntity”]//label[text()=“sector”]//以下同级::*')查找元素。text
除无任何例外:
扇区=-1
尝试:
revenue=驱动程序。通过xpath('.//div[@class=“infoEntity”]//label[text()=“revenue”]//以下同级::*')查找元素。text
除无任何例外:
收入=-1
尝试:
竞争者=驱动程序。通过xpath('.//div[@class=“infoEntity”]//labe)查找元素