我试图使用python中的selnium连接Twitter。我无法使用名称或Xpath连接。通过单击“检查”,然后复制特定元素的xpath来复制xpath。我发现的关于连接Twitter的所有教程都是旧的,不相关的。我把密码附在这里。我在@id="layers"
上出错了
代码的图像:
我很乐意帮忙。
代码:
from threading import Thread
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.support import wait
driver=webdriver.Chrome(executable_path="C:\\Webdrivers\\chromedriver.exe")
driver.get("https://twitter.com/i/flow/login")
search=driver.find_element_by_xpath("//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[5]/label/div/div[2]/div/input")
search.send_keys("orlaharty1@gmail.com")
button=driver.find_element_by_xpath("//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[6]/div")
button.click()
发布于 2021-12-05 18:25:41
你用了两次双引号。相反,将xpath粘贴到单引号'xpathblabla'
中,添加driver.implicity_wait(seconds)
,这样,如果驱动程序正在获取尚未加载的元素,则不会出现任何错误。
driver.get("https://twitter.com/i/flow/login")
#add this line
driver.implicitly_wait(10)
# single quotes
search=driver.find_element_by_xpath('//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[5]/label/div/div[2]/div/input')
search.send_keys("orlaharty1@gmail.com")
button=driver.find_element_by_xpath('//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[6]/div')
button.click()
发布于 2021-12-05 19:19:42
在构建xpath时,有两种方法,您可以遵循其中任何一种方法:
"..."
)中传递xpath的值,在单引号(即'...'
)中传递属性值。例如:
search=driver.find_element_by_xpath("//*@attribute_name='attribute_value'") #注意^双引号&^单引号'...'
)中传递xpath的值,在双引号(即"..."
)中传递属性值。例如:
search=driver.find_element_by_xpath('//*@attribute_name="attribute_value"') #注意^单引号和^双引号解决方案
在上述两次会议之后,您的有效代码将是:
driver.get("https://twitter.com/i/flow/login")
search=driver.find_element_by_xpath("//*[@id='layers']/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[5]/label/div/div[2]/div/input")
search.send_keys("orlaharty1@gmail.com")
button=driver.find_element_by_xpath("//*[@id='layers']/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[6]/div")
button.click()
https://stackoverflow.com/questions/70236998
复制相似问题