Home > Software engineering >  How to access button of div that has no name attribute in selenium web driver?
How to access button of div that has no name attribute in selenium web driver?

Time:12-28

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

CodePudding user response:

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

CodePudding user response:

In Chrome you can bring up the inspector by right-mouse clicking on the element of interest (the Next button) and selecting Insepct 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()

CodePudding user response:

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