【问题标题】:Python Selenium Able to Find Elements But Can't Find them Using Python [duplicate]Python Selenium能够找到元素但无法使用Python找到它们[重复]
【发布时间】:2020-08-28 00:24:13
【问题描述】:

我很擅长使用 python selenium,但它在新的 facebook 设计页面上不起作用

如果有人有新的 facebook 页面,请检查您是否可以在此页面上使用 python 检测到任何元素:https://www.facebook.com/messages/t/

例如寻找这个 xpath ://li[@class='_5l-3 _1ht1 _6zk9'],在浏览器中我们会找到 30 个元素。但是在 python 中,当我们运行我们的脚本时它不会工作:

这是我的代码:

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

opt = Options()
opt.add_argument("--disable-infobars")
opt.add_argument("start-maximized")
opt.add_experimental_option("prefs", {
        "profile.default_content_setting_values.media_stream_mic": 2,
        "profile.default_content_setting_values.media_stream_camera": 2,
        "profile.default_content_setting_values.geolocation": 2,
        "profile.default_content_setting_values.notifications": 2
    })
global driver
driver = webdriver.Chrome("yourpathtochromedriver", options=opt)

time.sleep(5)
driver.get("https://www.facebook.com/")

username = driver.find_element_by_xpath("//input[@name='email']")
username.send_keys('youremail')
username = driver.find_element_by_xpath("//input[@name='pass']")
username.send_keys('yourpassword')
driver.find_element_by_xpath("//button[@name='login']").click()

driver.get("https://www.facebook.com/messages/t")
time.sleep(3)

test = driver.find_elements_by_xpath("//li[@class='_5l-3 _1ht1 _6zk9']")
count = (len(test))
print(count)

【问题讨论】:

  • 它会抛出任何错误吗?

标签: python selenium


【解决方案1】:

我之前遇到过类似的问题,对我有用的是使用 WebDriverWait 类。根据我的经验,发生此问题是因为您试图在页面加载之前从页面中检索元素。您可以使用WebDriverWait 等待检测到元素的存在然后检索它。

【讨论】:

  • 你好 Salty,我不认为这是问题所在,我已经用 webdriver 尝试过,页面已全部加载,但仍然无法检测到元素
  • 哦,他们我不知道如何帮助,对不起:(
【解决方案2】:
  1. 我在 https://www.facebook.com/messages/t/ 页面上找不到元素 "//li[@class='_5l-3 _1ht1 _6zk9']"
    请提供更多错误日志。

  2. 这不是一个好主意"//li[@class='_5l-3 _1ht1 _6zk9']",试试这样的"//li[contains(@class,'_5l-3') and contains(@class,'_1ht1') and contains(@class,'_6zk9')]",或者cssSelector表达式"li._5l-3._1ht1._6zk9"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    • 2016-08-31
    • 1970-01-01
    相关资源
    最近更新 更多