错误消息:NoSuchElementException:消息:没有这样的元素:无法找到元素:{“方法”:“ css选择器”,“选择器”:“ ._ 5qtp”}

时间:2020-08-06 19:34:59

标签: css python-3.x facebook web-scraping selenium-chromedriver

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

我从某个网站复制了它以了解硒。 在这里,Web驱动程序找不到我们在其中写入状态的框的类或ID。

我什至在检查面板中搜索了它,但没有得到正确的ID或类,并且提示了上述错误。请帮帮我!

如何找到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

1 个答案:

答案 0 :(得分:2)

首先,我建议您在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,如下所示: How to copy 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()


顺便说一句,这是通过鼠标选择元素的方法,在下图中,您可以看到“用户名”字段的CSS选择器名称: (如果在检查器打开的情况下右键单击任何元素,则可以找到Copy>完整的XPath来复制XPath)

Using Inspector tool for showing css selector item