错误消息:NoSuchElementException:消息:没有这样的元素:无法找到元素:{"method":"css selector","selector":"._5qtp"}

Error Message: NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"._5qtp"}

我正在尝试使用以下代码在 Facebook 上使用 selenium 自动更新状态。

我从一些网站上复制了它来了解硒。 此处网络驱动程序无法找到 class 或我们在其中写入状态的框的 ID。

我什至用检查面板搜索它,但我没有得到任何正确的ID或class并提示上述错误。请帮助我!

如何找到状态更新框的 XPath 或 ID?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options

import time
### login details ########################
username = "xxxxxxx@xxxxx.xxx"
pwd = "xxxxxxxxx"
##########################################

### what is on my mind ###################
msg = "hey there!"
##########################################

# initializing driver and loading web
chrome_path = r"chromedriver.exe"
options = Options()
options.add_argument("--disable-notifications")
driver = webdriver.Chrome(chrome_path, chrome_options=options)
driver.get("http://www.facebook.com/")
# end initializing

# start login
user_input = driver.find_element_by_xpath("""//*[@id="email"]""")
pwd_input = driver.find_element_by_xpath("""//*[@id="pass"]""")
user_input.send_keys(username)
pwd_input.send_keys(pwd)
pwd_input.send_keys(Keys.ENTER)
# end login

# writing msg
time.sleep(3)
first_what_is_on_my_mind_element = driver.find_element_by_class_name("_5qtp")
first_what_is_on_my_mind_element.click()
time.sleep(3)
second_what_is_on_my_mind_element = driver.switch_to.active_element
second_what_is_on_my_mind_element.send_keys(msg)
# end writing

# posting
buttons = driver.find_elements_by_tag_name('button')
for button in buttons:
    if 'Post' in button.text:
        button.click()
# end posting

首先,我建议您在 shell 中逐步进行工作并测试正在进行的代码。

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options

#This part is for opening the webdriver with proxy (since FB is prohibited here!)
proxy="127.0.0.1:0000"
chrome_options=webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=socks5://' + proxy)
driver=webdriver.Chrome(options=chrome_options)

# Let's open the FB and insert Username and Password:
driver.get("https://www.facebook.com")

# Give the username by copying the full XPath and send keys:
driver.find_element_by_xpath("/html/body/div[1]/div[3]/div/div/div/div/div[2]/form/table/tbody/tr[2]/td[1]/input").send_keys("Username") 

#Same for the Password:
driver.find_element_by_xpath("/html/body/div[1]/div[3]/div/div/div/div/div[2]/form/table/tbody/tr[2]/td[2]/input").send_keys("Password")

#Click the login button, you also can send Return key to password field:
driver.find_element_by_xpath("/html/body/div[1]/div[3]/div/div/div/div/div[2]/form/table/tbody/tr[2]/td[3]/label/input").click()

#Hitting the status bar:
driver.find_element_by_xpath("/html/body/div[1]/div/div/div[1]/div[3]/div/div/div[1]/div[1]/div/div[2]/div/div/div[3]/div/div[2]/div/div/div/div[1]/div/div[1]/div").click() 

#giving the status message:
driver.find_element_by_xpath("/html/body/div[1]/div/div/div[1]/div[4]/div/div/div[1]/div/div[2]/div/div/div/form/div/div[1]/div/div[2]/div[2]/div[1]/div[1]/div[1]/div/div/div/div/div[2]/div/div/div/div").send_keys("This is a test status from an automation :)")

#Hit the Post button:
driver.find_element_by_xpath("/html/body/div[1]/div/div/div[1]/div[4]/div/div/div[1]/div/div[2]/div/div/div/form/div/div[1]/div/div[2]/div[3]/div[2]/div").click()

#Enjoy the code :)

我已经测试了上面的代码,它运行良好。您可以找到 Full-Xpath ,如下所示:


已编辑: 首先,忘记登录页面的 XPath。因为它是相对于浏览器情况而言的。相反,使用 CSS 选择器,如下代码所示: (它还有用于禁用通知的 webdriver 选项,这可能会解决找不到状态元素的错误)

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options

username=str(input("Please enter your FB username:\n"))
password=str(input("Please enter your FB password:\n"))
proxy=str(input("Please enter your proxy:\n"))


#This part is for opening the webdriver with proxy (since FB is prohibited here!)
chrome_options=webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=socks5://' + proxy)

# Making Notifications off automatically
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
driver=webdriver.Chrome(options=chrome_options)

# Let's open the FB and insert Username and Password:
driver.get("https://www.facebook.com")

# Give the username by copying the css_selector and send keys:
driver.find_element_by_css_selector("input#email.inputtext.login_form_input_box").send_keys(username)
#Same for the Password:
driver.find_element_by_css_selector("input#pass.inputtext.login_form_input_box").send_keys(username)

#Click the login button, you also can send Return key to password field:
driver.find_element_by_css_selector("label#loginbutton.login_form_login_button.uiButton.uiButtonConfirm").click()
#Hitting the status bar:
driver.find_element_by_xpath("/html/body/div[1]/div/div/div[1]/div[3]/div/div/div[1]/div[1]/div/div[2]/div/div/div[3]/div/div[2]/div/div/div/div[1]/div/div[1]/div").click() 

#giving the status message:
driver.find_element_by_xpath("/html/body/div[1]/div/div/div[1]/div[4]/div/div/div[1]/div/div[2]/div/div/div/form/div/div[1]/div/div[2]/div[2]/div[1]/div[1]/div[1]/div/div/div/div/div[2]/div/div/div/div").send_keys("This is a test status from an automation :)")

#Hit the Post button:
driver.find_element_by_xpath("/html/body/div[1]/div/div/div[1]/div[4]/div/div/div[1]/div/div[2]/div/div/div/form/div/div[1]/div/div[2]/div[3]/div[2]/div").click()


顺便说一句,这是通过鼠标 select 元素的方法,在下图中您可以看到 CSS 用户名字段的选择器名称: (如果你 right-click 在任何元素上,当检查器打开时,你可以找到 Copy>full XPath 来复制 XPath)