1

I have been trying this for a while and searched different forums but I couldn't find any snippet to do this.

I have a report in which options need to be selected through dropdown using Selenium in python. Below is the HTML structure

<div align="center"> Select Fruit</div>
<p align="center"><br>
	<span id="0e6b87875e914a5f8d72bbee6844bea3" style="color: black; font-family: Arial; font-size: 13px; font-weight: normal; font-style: normal; width: 113px; display: inline-block;" class="sf-element sf-element-control sfc-property sfc-dropdown">
		<div class="sf-element sf-element-dropdown" title="" style="position: relative; width: 100px;">
		<div class="sf-element sf-element-icon" style="position: absolute; top: 1px; left: 91px; height: 17px; width: 17px;">
			<svg width="17px" height="17px"><path d="M4,6 l7,0 l-3.5,3.5 z" fill="currentColor" stroke-width="1" transform="scale(1.1333333333333333,1.1333333333333333)" class="Down"></path></svg>
		</div>
		<div class="sf-element sf-element-text-box" style="display: inline-block; word-wrap: break-word; width: 83px;">(None)</div>
			<select class="sf-dropdown-select" style="color: rgb(0, 0, 0); font-family: Arial; background-color: rgb(248, 248, 248);">
				<option value="0" selected="selected">(None)</option>
				<option value="1">Apple</option>
				<option value="2">Mango</option>
				<option value="3">Grapes</option>
			</select>
		</div>
	</span><br></p>

I have tried different ways using css selector and XPath but nothing seems to work. Below is the code I tried

driver.find_element_by_xpath('//*[@id="0e6b87875e914a5f8d72bbee6844bea3"]/div/select/option[@value = "Mango"]')

Also different variants like options[2] and using css selector but it always give NoSuchElementException.

Can someone please share some insights on this?

Thanks

3 Answers 3

1

Add text()="Mongo" instead of @value="Mongo"

 driver.find_element_by_xpath('//[@id="0e6b87875e914a5f8d72bbee6844bea3"]/div/select/option[text() = "Mango"]').click()
Sign up to request clarification or add additional context in comments.

2 Comments

Its giving me new error : InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //[@id="0e6b87875e914a5f8d72bbee6844bea3"]/div/select/option[text ="257"] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//[@id="0e6b87875e914a5f8d72bbee6844bea3"]/div/select/option[text ="257"]' is not a valid XPath expression. (Session info: chrome=43.0.2357.81) (Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 6.1 SP1 x86_64)
could you please double check the xpath of element? is that same ?
0

The dropdown is clearly within <Select> tag. Hence it would be convinient to use the Select Class as follows:

//import
from selenium.webdriver.support.ui import Select
//code block
selectOption = Select(driver.find_element_by_class_name("sf-dropdown-select"))
selectOption.select_by_visible_text("Mango")

Comments

0

When dealing with SELECT elements, there is a helper class, Select, that makes it a lot easier.

select = Select(driver.find_element_by_css_selector("#0e6b87875e914a5f8d72bbee6844bea3 select"))
select.select_by_visible_text("Mango")

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.