0

Here we have 2 dropdown in the webpage. I am able to select the first one, but while trying to select option in second dropdown. Second dropdown options depend upon the first one. Link I used for testing < https://tgtransport.net/TGCFSTONLINE/reports/applicationstatussearch.aspx>

Code I have tried:

# select registration from first dropdown
select_item = driver.find_element(By.XPATH, '//*[@id="ctl00_OnlineContent_ddlModule"]')
select = Select(select_item)
reg_element = driver.find_element(By.CSS_SELECTOR, '#ctl00_OnlineContent_ddlModule > option:nth-child(2)')
select.select_by_visible_text('REGISTRATION')
assert reg_element.is_selected()

try:
    myElem = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="ctl00_OnlineContent_ddlInput"]/option[3]')))
    print("Page is ready!")
except TimeoutException:
    print("Loading took too much time!")

# select registration no from second dropdown
driver.find_element(By.XPATH, '/html/body/form/table[2]/tbody/tr[1]/td/div[2]/div/table/tbody/tr[3]/td[2]/select/option[3]').click()

driver.find_element(By.ID, "ctl00_OnlineContent_txtInput").click()
driver.find_element(By.ID, "ctl00_OnlineContent_txtInput").send_keys("TS07JS1701")

Error I am getting: -

Traceback (most recent call last):
  File "C:\Users\Sonu\PycharmProjects\pythonProject\main.py", line 86, in <module>
    driver.find_element(By.XPATH, '/html/body/form/table[2]/tbody/tr[1]/td/div[2]/div/table/tbody/tr[3]/td[2]/select/option[3]').click()

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

[Reference I have used - ] https://github.com/SeleniumHQ/seleniumhq.github.io/blob/trunk//examples/python/tests/support/test_select_list.py#L9-L10 Tried all possible ways to solve it, but couldn't. Please help me to find out the solution.

1 Answers1

0

The suggested thread helped me to find out my answer. I am posting my code below for future reference. I made some execution to wait to get the full page using time.sleep() method.

# select registration from first dropdown
select_item = driver.find_element(By.XPATH, '//*[@id="ctl00_OnlineContent_ddlModule"]')
select = Select(select_item)
reg_element = driver.find_element(By.CSS_SELECTOR, '#ctl00_OnlineContent_ddlModule > option:nth-child(2)')
select.select_by_visible_text('REGISTRATION')
assert reg_element.is_selected()

try:
    myElem = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="ctl00_OnlineContent_ddlInput"]/option[3]')))
    print("Page is ready!")
except TimeoutException:
    print("Loading took too much time!")
time.sleep(10)
# select registration no from second dropdown
driver.find_element(By.XPATH, '/html/body/form/table[2]/tbody/tr[1]/td/div[2]/div/table/tbody/tr[3]/td[2]/select/option[3]').click()
time.sleep(5)
driver.find_element(By.CSS_SELECTOR, '#ctl00_OnlineContent_txtInput').click()
driver.find_element(By.CSS_SELECTOR, '#ctl00_OnlineContent_txtInput').send_keys("*****")