获取与另一个xpath查询相关的元素

问题描述

from selenium import webdriver
from selenium.webdriver.support.ui import webdriverwait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
# from bs4 import BeautifulSoup
# import pandas as pd

driver = webdriver.Chrome("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe")

driver.get("https://www.seek.com.au/jobs?where=Work%20from%20home")
assert "SEEK" in driver.title

location = webdriverwait(driver,25).until(
    ec.visibility_of_all_elements_located((By.XPATH,"//span[contains(text(),'location:"
                                                     " Melbourne')]")))

for loc in location:
    print(loc.text)
    job = driver.find_elements_by_xpath("//span/h1/a").text
    print(job)


driver.close()

所以我有loc.text在HTML文档中找到一个特定的字符串(位于Here)

我正在尝试并且已经尝试了一段时间的是: 如果位置匹配(例如,墨尔本),则从href链接(文本和Href)中获取数据,并将其放在该位置下。

Administration Manager (Part time / Virtual office / Work from home)
location: Melbourne
Administration Manager (Part time / Virtual office / Work from home)
location: Melbourne
Administration Manager (Part time / Virtual office / Work from home)
location: Melbourne
Administration Manager (Part time / Virtual office / Work from home)
location: Melbourne
Administration Manager (Part time / Virtual office / Work from home)
location: Melbourne
Administration Manager (Part time / Virtual office / Work from home)

Process finished with exit code 0

但是我在运行元素时遇到问题,因此使用元素的原因(无法将其从Selenium代码转换为文本,它显示所有20个链接,无法具体显示我需要的链接,等等)

千古流传,学习XPATH功能...现在,我被困在这个无数的Google搜索上,后来又有了StackOverflow链接,我比自己想要的要沮丧得多。

这里缺少什么简单的解决方案?

感谢您的所有帮助。

解决方法

要获取位置MelbourneJob header,您需要诱导WebDriverWait()并等待visibility_of_all_elements_located()并按照xpath来获取父列表,然后迭代循环检查位置Melbourne,然后打印Job header

代码

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

driver = webdriver.Chrome()
driver.get("https://www.seek.com.au/jobs?where=Work%20from%20home")
elements=WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.XPATH,"//div[@data-automation='searchResults']//article")))

for ele in elements:
    if "Melbourne" in ele.find_element_by_xpath(".//span[@class='Eadjc1o' and contains(.,'location')]").text:
        print(ele.find_element_by_xpath(".//span[@class='Eadjc1o' and contains(.,'location')]").text)
        print(ele.find_element_by_xpath(".//h1/a").text)
        print("======================================")

控制台输出:

location: Melbourne
Administration Manager (Part time / Virtual office / Work from home)
======================================
location: Melbourne
Customer Care Consultant - Work From Home
======================================
location: Melbourne
Occupation Coordinator
======================================
location: Melbourne
Planner
======================================
location: Melbourne
Part Time - Customer Contact Officer
======================================
location: Melbourne
Administration Assistant - Dispute Resolution (part time)
======================================