Reputation: 43
I tried to connect to Twitter using selnium in python.
I could not connect using Name or Xpath.
The xpath is copied by clicking Inspect and then copy xpath of the specific element.
All the tutorials I found regarding connecting to Twitter are old and irrelevant.
I enclose the code here. I have error on @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("[email protected]")
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()
Upvotes: 0
Views: 11876
Reputation: 11
Since xpath usage has changed in selenium, you should use it in the following style. Remember to use single quotes inside double quotes.
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
site = webdriver.Edge()
site.get("https://www.test.com")
sleep(15)
email = site.find_element(By.XPATH, "//*[@id='ap_email']")
email.send_keys("[email protected]")
Upvotes: 1
Reputation: 415
You are using double quotes twice. Instead paste the xpath to single quotes 'xpathblabla'
Also, add driver.implicity_wait(seconds)
so you won't get any errors if your driver is fetching elements that aren't loaded yet
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("[email protected]")
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()
Upvotes: 2
Reputation: 193108
While constructing an xpath 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 i.e. "..."
and the value of the attributes in single quotes i.e. '...'
. 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 i.e. '...'
and the value of the attributes in double quotes i.e. "..."
. As an example:
search=driver.find_element_by_xpath('//*[@attribute_name="attribute_value"]')
# note the ^single quote & the ^double quote
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("[email protected]")
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()
Upvotes: 0