简体   繁体   English

使用xpath selenium查找元素

[英]find element with xpath selenium

I'm following this tutorial in order to learn how to build a webscraper to get jobs listings. 我正在学习本教程 ,以学习如何构建一个webscraper来获取工作列表。 Now I'm trying to follow along with another website. 现在我想跟随另一个网站。 I run into the problem that I don't know how to extract the links of the individual job listings. 我遇到了一个问题,我不知道如何提取单个工作列表的链接。

When I'm inspecting the page I've found the element I need 当我正在检查页面时,我发现了我需要的元素 在此输入图像描述

When I copy the xpath and use it in my code I get an error. 当我复制xpath并在我的代码中使用它时,我收到一个错误。 What am I doing wrong? 我究竟做错了什么?

import selenium 
base_url = "https://www.nationalevacaturebank.nl"     
start_url = "https://www.nationalevacaturebank.nl/vacature/zoeken?query=&location=&distance=city&limit=100&sort=relevance&filters%5BcareerLevel%5D%5B%5D=Starter&filters%5BeducationLevel%5D%5B%5D=MBO" 
driver = webdriver.Firefox()    
elem = driver.find_element_by_xpath("//*[@id="search-results-container"]/div/div[1]/div[2]/article/job/a")

>>NoSuchElementException: Message: Unable to locate element: //*[@id="search-results-container"]/div/div[1]/div[2]/article/job/a

First, you should be connecting to your website... 首先,您应该连接到您的网站......

Second, you should use waits you can read about it here 其次,你应该使用waits你可以在这里阅读它

the code should look something like: 代码应该类似于:

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


base_url = "https://www.nationalevacaturebank.nl"     
start_url = "https://www.nationalevacaturebank.nl/vacature/zoeken?query=&location=&distance=city&limit=100&sort=relevance&filters%5BcareerLevel%5D%5B%5D=Starter&filters%5BeducationLevel%5D%5B%5D=MBO" 
my_xpath = '//*[@id="search-results-container"]/div/div[1]/div[2]/article/job/a'
driver = webdriver.Firefox()
driver.get(start_url)  
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH, my_xpath)))

Edit 编辑

I order to get all the links of the individual job listing you can create a list of the links and append theme: 我命令获取单个职位列表的所有链接,您可以创建链接列表并追加主题:

wait = WebDriverWait(driver, 10)
elements = wait.until(EC.presence_of_all_elements_located((By.XPATH, '//*[@id="search-results-container"]//article/job/a')))
list_of_links = []
for i in elements:
    list_of_links.append(i.get_attribute('href'))
    # print(f"link = {i.get_attribute('href')}")
print(list_of_links)

Hope this helps you! 希望这对你有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM