0

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:

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

3 Answers 3

2

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()
Sign up to request clarification or add additional context in comments.

1 Comment

Based on recent selenium update and deprecation of the find_element_by_xpath function here is the modifuied code driver.get("https://twitter.com/i/flow/login") search=driver.find_element('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('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()
1

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]")

Comments

0

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

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

1 Comment

Based on recent selenium update and deprecation of the find_element_by_xpath function here is the modifuied code driver.get("https://twitter.com/i/flow/login") search=driver.find_element('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('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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.