简体   繁体   English

find_element_by_xpath() 使用 Selenium 和 Python 显示语法错误

[英]find_element_by_xpath() shows syntax error using Selenium with Python

I tried to connect to Twitter using selnium in python.我尝试使用 python 中的 selnium 连接到 Twitter。 I could not connect using Name or Xpath.我无法使用名称或 Xpath 进行连接。 The xpath is copied by clicking Inspect and then copy xpath of the specific element.通过单击检查复制 xpath,然后复制特定元素的 xpath。 All the tutorials I found regarding connecting to Twitter are old and irrelevant.我发现的所有关于连接到 Twitter 的教程都是陈旧且无关紧要的。 I enclose the code here.我在这里附上代码。 I have error on @id="layers"我在@id="layers"上有错误

Image of the code:代码图片:

代码图像

I would be very happy to help.我很乐意提供帮助。

Code:代码:

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()

You are using double quotes twice.您使用了两次双引号。 Instead paste the xpath to single quotes 'xpathblabla' Also add driver.implicity_wait(seconds) so you wont get any erros if your driver is fetching elements that aren't loaded yet而是将 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()

While constructing an there are two approaches and you can follow any one of them:在构建时,有两种方法,您可以遵循其中任何一种方法:

  • You need to pass the value of the xpath with in double quotes ie "..." and the value of the attributes in single quotes ie '...' .您需要将xpath的值用双引号(即"..."和单引号(即'...'的属性值传递。 As an example:举个例子:

     search=driver.find_element_by_xpath("//*[@attribute_name='attribute_value']") # note the ^double quote & the ^single quote
  • You need to pass the value of the xpath with in single quotes ie '...' and the value of the attributes in double quotes ie "..." .您需要用单引号传递xpath的值,即'...'和双引号中的属性值,即"..." As an example:举个例子:

     search=driver.find_element_by_xpath('//*[@attribute_name="attribute_value"]') # note the ^single quote & the ^double quote

Solution解决方案

Following the above two convensions discussed above, your effective lines of code will be:按照上面讨论的上述两个约定,您的有效代码行将是:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM