0

Trying to use Selenium to select a country and state through a dropdown. Managed to get it to fill out a few forms like first name, last name etc. Getting an "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="7f7a846e-ce26-49f2-b60d-202ac1a355f4"]"}" error. Any suggestions as to what's causing this?

Code is as follows:

import selenium
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import Select

driver = selenium.webdriver.Chrome(executable_path='/PATH')


def get_url(url):
    driver.get(url)
    driver.maximize_window()


def fill_data():
    # Find the signup button
    Wait(driver, 30).until(expected_conditions.presence_of_element_located
                           ((By.ID, 'signup-button'))).click()

    # Find the email name box
    email_name = Wait(driver, 30).until(expected_conditions.visibility_of_element_located
                                        ((By.XPATH, "/html/body/onereg-app/div/onereg-form/div/div/form/section/"
                                                    "section[1]/onereg-alias-check/fieldset/onereg-progress-meter"
                                                    "/div[2] "
                                                    "/div[2]/div/pos-input[1]/input"))).send_keys('Test')

    # Select gender
    driver.find_element_by_xpath('/html/body/onereg-app/div/onereg-form/div/div/form/section'
                                 '/section[2]/onereg-progress-meter/onereg-personal-info'
                                 '/fieldset/div/div/onereg-radio-wrapper[2]/pos-input-radio/label/i').click()

    # Fill out first and last name
    Wait(driver, 30).until(expected_conditions.element_to_be_clickable
                           ((By.XPATH, "//input[@data-test='first-name-input']"))).send_keys("Test")
    Wait(driver, 30).until(expected_conditions.element_to_be_clickable
                           ((By.XPATH, "//input[@data-test='last-name-input']"))).send_keys("Test")

    # Select country and state
    country = Select(driver.find_element_by_xpath('//*[@id="7f7a846e-ce26-49f2-b60d-202ac1a355f4"]'))
    country.select_by_visible_text('Thailand')
    state = Select(driver.find_element_by_xpath('//*[@id="409399e5-b61b-4b74-9ef7-7a511e29f339"]'))
    state.select_by_visible_text('New York')


get_url('https://www.mail.com/')
fill_data()
  • There is a possibility in id change.. Because the format of id is looking like a not unique one.. Try to use some other xpath format.. – Satish Rongala Nov 01 '19 at 09:39

2 Answers2

2

The xpath value of country and state looks like they are autogenerated. If you want to identify it, I would suggest you use a Locator Strategy which is unique.

You may check the values of the following attributes to see which is unique and easy to understand.

ID,Name,Class Name,Tag Name,Link Text,Partial Link Text,XPATH

Coming back to your question:

"Find Element command" throws NoSuchElementException if it does not find the element matching the criteria. This is clearly the case with yours as it is mentioned in the stack trace itself.

So use a unique identifier other than xpath. OR If you want to avoid the exception in case if no elements are found, you may use the "FindElements command".

Syntax:

List<WebElement> elementName = driver.findElements(By.LocatorStrategy("LocatorValue"));

Example:

List<WebElement> listOfElements = driver.findElements(By.xpath("//div"));
shihabudheenk
  • 593
  • 5
  • 18
0

I see that you are using xpath of " # Select country and state" by an alphanumeric id (i.e @id="7f7a846e-ce26-49f2-b60d-202ac1a355f4"] and @id="409399e5-b61b-4b74-9ef7-7a511e29f339"]'). which is causing the problem as alphanumeric one tends to change randomly.

You can use any other relative xpath which does not change and the error will be easily avoided. Please paste the html code of the section for country and state and I will try to write the relative xpath which is not autogenerated.