selenium 中 xpath 路径正确,但无法找到元素怎么回事?已经显示等待了而且页面已经出来了。

selenium 中 xpath 路径正确,但无法找到元素怎么回事?已经显示等待了而且页面已经出来了。

el = WebDriverWait(driver, timeout=10).until(
        lambda d: d.find_element(By.XPATH, "//div[@class='sec-operate']//div[@class='ui-dropmenu-label']"))

img

img

被iframe包住了可恶

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这个问题的回答你可以参考下: https://ask.csdn.net/questions/697569
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:震惊!xpath定位不到对应元素节点
  • 除此之外, 这篇博客: selenium自动化测试原来这么简单?中的 通过Xpath来定位元素 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:
    driver.find_element_by_xpath(元素的xpath)
    driver.find_elements_by_xpath(元素的xpath)
    

    在这里插入图片描述

    # 根据相对路径定位
    driver.find_element_by_xpath("//form[@id='form']/span/input").send_keys("为什么")
    #根据元素id定位
    driver.find_element_by_xpath("//input[@id='kw']").send_keys("为什么")
    #根据元素name定位
    driver.find_element_by_xpath("//input[@name='wd']").send_keys("为什么")
    #根据元素class定位
    driver.find_element_by_xpath("//input[@class='s_ipt']").send_keys("为什么")
    # 根据元素标签之间文本定位
    driver.find_element_by_xpath("//a[contains(text(),'新闻')]").click()
    # 标签的某个属性以什么开头来定位
    driver.find_element_by_xpath("//a[starts-with(@href,'http')]").click()
    #a标签的href属性以heep开头并且文本内容为‘新闻’来定位
    # driver.find_element_by_xpath("//a[starts-with(@href,'http')][contains(text(),'新闻')]").click()
    #通过两个属性定位
    driver.find_element_by_xpath("//input[@id='kw' and @name='wd']")
    driver.find_element_by_xpath("//input[@id='kw' or @name='wd']")
    
  • 您还可以看一下 CSDN就业班老师的第一章:数据提取与清洗策略课程中的 百度针对xpath爬虫的反爬策略与解决方式小节, 巩固相关知识点

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^