【问题标题】:Copied XPATH doesn't work in Selenium Python复制的 XPATH 在 Selenium Python 中不起作用
【发布时间】:2021-01-19 14:28:11
【问题描述】:

我试图抓取我学校的网站,但它无法正常工作。我尝试了很多东西,但大多数时候我通过的 XPATH 是不正确的。 (XPATH 是从 HTML 复制而来的)有很多这样的元素在单击后没有响应,或者无法找到它们。 我的代码:

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

def element(driver, by_x, html_element):
    try:
        element = WebDriverWait(driver, 5).until(
            EC.presence_of_element_located((by_x, html_element))
        )
        return element
    except:
        print("Element not found")

class EduPage:
    def __init__(self, name, password, edu_link):
        options = webdriver.ChromeOptions()
        options.add_experimental_option("excludeSwitches", ["enable-logging"])
        PATH = "C:\Programming Modules\Drivers\chromedriver.exe"
        self.driver = webdriver.Chrome(executable_path=PATH, options=options)
        self.name = name
        self.password = password
        self.driver.get(edu_link)

    def log_in(self):
        login_name = element(self.driver, By.ID, "login_Login_1e1")
        login_name.send_keys(self.name)
        login_pass = element(self.driver, By.ID, "login_Login_1e2")
        login_pass.send_keys(self.password, Keys.RETURN)

    def go_to_class(self):
        online_class = element(self.driver, By.XPATH, "//*[@id='jwb98b9ff6_md']/div/div[2]")
        online_class.click()


edupage = EduPage("my name", "my password", "https://gymstrop.edupage.org/login/")
edupage.log_in()
edupage.go_to_class

HTML 代码(我想点击突出显示的元素)

【问题讨论】:

    标签: python selenium web-scraping xpath webdriver


    【解决方案1】:

    失败的原因是parent element id attribute动态。我建议使用以下xpath 来识别元素..

    //div[@class='userTopOnlineLesson']/a
    

    所以你的代码会是这样的

    online_class = element(self.driver, By.XPATH, "//div[@class='userTopOnlineLesson']/a")
    

    或跟随 css 选择器。

    div.userTopOnlineLesson>a.userTopBlackboard
    

    【讨论】:

    • @themm1 :你遇到了什么错误?超时?
    【解决方案2】:

    KunduK 提供的解决方案应该可以工作,如果它不工作,我也想看看你得到的实际 selenium 错误。

    同时请您尝试将等待时间增加到60,只是为了弄清楚这是否与等待时间长无关

    try:
            element = WebDriverWait(driver, 60).until(
                EC.presence_of_element_located((by_x, html_element))
            )
            return element
        except:
            print("Element not found")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-30
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2017-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多