1

I have a problem because I would like to get the selected option name: Option3 in this case. I want to use assert to check if value is selected correctly in this case. You can see a part of my page below:

<html>
	<body>
		<table border="0" cellpadding="0" cellspacing="0" class="rich-toolbar " id="mainMenuToolbar" width="100%">
			<tbody>
				<tr valign="middle">
					<td class="rich-toolbar-item  " style=";">
						<form id="substituteForm" name="name" method="post" action="http://homepage/home.seam" enctype="application/x-www-form-urlencoded">
							<select name="substituteForm:j_id158" size="1" onchange="document.getElementById(&#39;substituteForm:substituteSubmit&#39;).click();">
								<option value="0">Option0</option>
								<option value="1">Option2</option>
								<option value="2" selected="selected">Option3</option>
							</select>
						</form>
					</td>
				</tr>
			</tbody>
		</table>
	</body>
</html>

I used DevTool to copy XPath and I wrote a code:

element = Select(driver.find_element_by_xpath("//*   [@id='substituteForm']/select"))

and I have error message:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //*[@id='substituteForm']/select

I tried many XPath combinations and it still doesn't work.

1 Answer 1

2

This seem to be timing issue, but not XPath

Try to use below code to wait until target select element appear in DOM:

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait

select = wait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//form[@id='substituteForm']/select")))
select.click()
selected_option = wait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//option[@selected='selected']")))
assert selected_option.text == "Option3"
Sign up to request clarification or add additional context in comments.

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.