我现在正在通过编写一个小脚本向facebook朋友发布生日评论来学习python和硒。我能够成功登录并导航到“朋友的生日”模式,但随后我无法在文本框中发表评论。
这是我遇到的错误:
selenium.common.exceptions.NoSuchElementException:
Message: no such element: Unable to locate element:
{"method":"xpath","selector":"//*[@id="u_i_6"]"}
我通过XPath查找其他页面元素没有任何问题,所以我不确定是什么引起了问题。我也尝试过找到通过类名进行注释的文本框,但结果相同。有人可以提供一些想法吗?
编辑:忘记粘贴代码
from selenium import webdriver
from pynput.keyboard import Key, Controller
import time
import config
# fetch facebook password from separate file in the same directory
pw = config.secret['pw']
keyboard = Controller()
driver = webdriver.Chrome()
options = webdriver.ChromeOptions()
options.add_argument('--disable-extensions')
options.add_argument('--disable-notifications')
# navigate to facebook url
driver.get('https://facebook.com')
# input email address
email_input = driver.find_element_by_xpath('//*[@id="email"]')
email_input.send_keys(<omitted>)
# input pw
password_input = driver.find_element_by_xpath('//*[@id="pass"]')
password_input.send_keys(pw)
# click login button element
login_button = driver.find_element_by_xpath('//*[@id="u_0_b"]')
login_button.click()
home_button = driver.find_element_by_xpath('//*[@id="u_0_c"]/a')
# wait 8 seconds for browser popup before pressing esc to get out
time.sleep(8)
keyboard.press(Key.esc)
# check if there are any birthdays today
birthday_section = driver.find_element_by_xpath('//*[@id="home_birthdays"]/div/div/div/div/a/div/div/span/span[1]')
birthday_section.click()
first_comment = driver.find_element_by_xpath('//*[@id="u_i_6"]')
first_comment.send_keys('happy birthday!')
# click post button to post comment
# close browser window after actions are complete
time.sleep(5)
driver.close()
答案 0 :(得分:1)
没有代码很难回答,但是这里有一些想法:
driver.get()
之后添加此代码以进行隐式等待:driver.implicitly_wait(5)
xpath
查找文本区域:"//form[contains(@action, 'birthday')]//textarea"
希望这对您有帮助,祝您好运!