2

I have written this code and want to log in to Twitter with a selenium web driver. But after filling in username I am unable to click the next button as it does not have name or id attributes. How can I do it?

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://twitter.com/login?lang=en-gb")
time.sleep(3)
driver.find_element_by_name('text').send_keys('username')
time.sleep(3)
driver.find_element_by_class_name('').click()

time.sleep(3)
print('TEst Completed')
1
  • you could try to use xpath or css selector to find that button, I haven't login into the page but something like: dirver.find_element_by_xpath("//button[text()='Next'") something like this should work Commented Dec 27, 2021 at 21:10

3 Answers 3

1

In Chrome you can bring up the inspector by right-mouse clicking on the element of interest (the Next button) and selecting Inspect from the popup). Then in the Inspector window, right-mouse click on the element you are interested in (it should already be highlighted), and select Copy and then Copy full XPath. You can then paste the result in to a find_element_by_xpath call.

As an aside, using time.sleep calls are fine if you want your program to run in slow motion so you can see what is happening, but if you want it to run at top speed, you should initially call driver.implcitly_wait(n) where n is some number of seconds (for example, 3). Then when you do a find_element call following driver.get, the driver will wait for up to 3 seconds for the desired element to appear before timing out but will not wait any longer than necessary.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())

try:
    driver = webdriver.Chrome()
    driver.maximize_window()
    # Wait for up to 3 seconds (but maybe less) for sought after
    # elemt to appear:
    driver.implicitly_wait(3)
    driver.get("https://twitter.com/login?lang=en-gb")
    driver.find_element_by_name('text').send_keys('username')
    driver.find_element_by_xpath('/html/body/div/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[6]/div/span/span').click()
    print('Test Completed')
except Exception as e:
    print("Got exception:", e)
finally:
    # Come here even on exceptions to be sure we "quit" the driver:
    input('Pausing until you hit enter...')
    driver.quit()

Update

enter image description here

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Sir please can you explain how you wrote this path driver.find_element_by_xpath('/html/body/div/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[6]/div/span/span').click()
Read the answer again. There I attempted to explain in complete detail how I came up with this path. I 've updated the answer adding a few screen captures,
1

To click on the element Next you can use either of the following Locator Strategies:

  • Using xpath and text():

    driver.find_element(By.XPATH, "//span[text()='Next']").click()
    
  • Using xpath and contains():

    driver.find_element(By.XPATH, "//span[contains(., 'Next')]").click()
    

Comments

1

No every element will have some fixed id or name or even class attribute.
In this particular case elements are having dynamic class names, so the most stable approach here will to use the Next text.
This can be done with XPath locator, as following:

driver.find_element_by_xpath("//span[text()='Next']").click()

Comments

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.