我正在尝试使用selenium和python自动发布博客
能够在浏览器中使用Inspect元素定位元素
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Firefox()
browser.get('https://www.blogger.com/go/signin')
email = browser.find_element_by_id('identifierId')
email.send_keys("xxxxxx@gmail.com")
email.send_keys(Keys.RETURN)
time.sleep(5)
password = browser.find_element_by_name("password")
password.send_keys("xxxxxx")
password.send_keys(Keys.RETURN)
time.sleep(5)
newpost = browser.find_element_by_partial_link_text("editor")
posttitle = browser.find_element_by_name("K3JSBVB-C-b titleField textField K3JSBVB-C-a")
错误
无法找到元素:[name =“ K3JSBVB-C-b titleField textField K3JSBVB-C-a”]
答案 0 :(得分:0)
此K3JSBVB-C-b
class attribute看起来很动态,我建议改用New post
文字。
相关的XPath表达式将类似于:
//a[text()='New post']
也考虑使用Explicit Wait,因为使用睡眠是antipattern you should be avoiding
new_post = WebDriverWait(browser, 10).until(
expected_conditions.element_to_be_clickable(By.XPATH("//a[text()='New post']")))
new_post.click()
更多信息:How to use Selenium to test web applications using AJAX technology