Select option robotframework - selenium

Really quick (but complicated?) question.
I have this:
<select multiple="multiple" id="id_products" class="selectmultiple" name="products">
<option value="3243">testproductP (3243)</option>
<option value="3244">testproductQ (3244)</option>
</select>
I need to robotframework with selenium to replicate that I select an option. However I can not find a keyword like "Select Option". So I tried using "Click Element" with an xpath pointing to the option.
Click Element xpath=//select[#name="products"]/option[#value=3244]
However this fails the test with the error: "timeout: timed out"
The xpath returns the correct element, but somehow it times out. Maybe Click Element is not supposed to be used like this, but I can't find a better keyword.
Any idea what's going on?

Click Element waits for a page load event unless you give it an additional parameter telling it not to wait. However, you should also be able to use the "Select From List" keyword.
Have fun!

You can use
Select From List ${XPATH} Value

Use " select element by value " keyword and specify the Xpath of the list dropdown and value of component which you want to select from list.

Select From List By Value Xpath=""/ID=""/Name="" Value

Try to select the element using Javascript. Example:
Execute Javascript document.querySelector("your css").click()

Related

Xpath finder for selenium using python -automation

I am trying to find an unique xpath for the below element, please advice if there are any better xpaths to make it for general text as I have currently given for that specific name.
<td tabindex="4" style="text-align: left;" title="name" class="">Name</td>
xpath i am using: //td[#title='name']
here if the name is changed with something else in the code, this wouldn't work, could someone help me identify unique xpath which works in general for any text. Thanks!
You can concatenate (using and / or) multiple attributes of element to find the element precisely .
By.xpath("//td[#title= 'name' and contains(text(), 'Name')]")
However we need to see more details of the code and your DOM of page to find element.
There will always be some element which will never change in the page(like name of table) using that as a relative point ,we can refer to the row of the table.
the simplest way to find the XPath of any element is to go to the developer options and select the markup of the element you want XPath of.
Right Click -> Copy -> XPath
I believe this is the simplest way. And you will also where you are doing wrong.
Screenshot attached for your reference.
I have used the general syntax - "//td[text()='{}']" and passing the name parameter when i define a method so that it won't be specific to one and others can test using the same locator with their name changed when someone else uses the testcase.
Thanks everyone for your response!

cssSelector("select[title=\"Sort By\"]")

In a selenium script Sorting a dropdown using
new Select(driver.findElement(By.cssSelector("select[title=\"Sort By\"]"))).selectByVisibleText("Name");
Can anybody please explain me this part cssSelector("select[title=\"Sort By\"]" of above statement.
Thanks!
cssSelector("select[title=\"Sort By\"]")
this is one of the technique to locate web element/elements.
You must have heard about xpath, which is one of the way to locate element/elements in a web page.
Further more , select is tag in HTML. title is attribute and Sort By is value of attribute.
Just like this :
HTML :
<select id="sel" class="drop-down" title="Sort By">
<options>..</options>
<options>..</options>
<options>..</options>
</select>
Now If you have to write cssSelector , you can write like this :
tagname[attribute="attribute value"]
select[id="sel"]
or
select[class="drop-dwon"]
or
select[title="Sort By"]
Hope this will be helpful !
new Select(driver.findElement(By.cssSelector("select[title=\"Sort By\"]"))).selectByVisibleText("Name");
You are selecting by CSS selector https://www.w3schools.com/cssref/css_selectors.asp. The alternative is XPath which is more powerful but harder to learn.
What this part By.cssSelector("select[title=\"Sort By\"]") does is select all select elements that have title attributes set equal to "Sort By". Although by prefixing driver.findElement( you are requesting just one element, the first. At least you would be if it was python, Java might differ but was not in your question nor tags.

Determining select from select multiple

This may have a simple answer but I could not find an answer by searching. I am using Selenium with Java.
I have several elements (WebElement ele). I locate them by means other than their direct tag names so I cannot simply use the xpath to answer the question I have.
I have several possible element types:
<div>
<input>
<input type = "checkbox">
<select>
<select multiple>
I can determine most of them. If I do ele.getTagName(). If it is a div I will know right away. If it is input I can do a ele.getAttribute("type") and see whether it is checkbox or not. But for select the tagname will give me select both for the regular select and the select multiple. There is no attribute name for the multiple, so how can I distinguish between select and select multiple ?
You could do something like this:
//if its a select element
Select se = new Select(ele);
Then you could simple check if this is a multi select element by running:
se.isMultiple()
You can use XPath expression to choose select with multiple attribute:
//select[#multiple]
or select without multiple attribute:
//select[not(#multiple)]
Also note that even without explicit value of multiple attribute, actual value is "true", so if multiple attribute present in select, ele.getAttribute("multiple") should return "true"

Locate element in selenium webdriver , if the id is changing time to time

<span id=":of" class="gO aQY" data-tooltip="Select Contacts" aria-label="To - Select Contacts" role="link" tabindex="1">To</span>
How to locate element in selenium webdriver, here id will change time to time. So I need to locate by the aria label" to-select contacts"
Get it by xpath:
//span[#aria-label="To - Select Contacts"]
Css selector is my favorite choice. There are so many different attributes you can use to locate your element without using its ID.
Please read this:
http://www.w3schools.com/cssref/css_selectors.asp
You can use css selectors. Smth like this:
//If class name is permanent and there is single instance on the page
driver.findElement(By.Css(".gO aQY"))
//otherwise
driver.findElement(By.Css("span[aria-label='To - Select Contacts']"))
See this link for more info.
You can also use this xpath
//span[#data-tooltip = 'Select Contacts']

how to click on element using WebDriver

i need to click on dat element:
<span id="act_action9" class="" onclick="openDialog('export', event)">
text
</span>
i cant click on id,because it is dynamic. After a click i am getting window with some settings.
You should use xpath in order to click this element, so you could add an expression that unequivocally identify this element.
Could you please include more HTML code, because just with the code included is not enough to create a xpath expression.
I recommend this tutorial to start doing good xpath expressions:
http://www.w3schools.com/xpath/
See example for dynamic xpath
By.xpath("//span[#id [contains(.,'act_action')]]")
Great xpath cheatsheets: http://extract-web-data.com/5-best-xpath-cheat-sheets-and-quick-references/
You can also use xpath like following(assuming only digit is dynamic):
".//span[contains(#id, 'act_action')]"
As ID is dynamic, you can use xpath for text which is inside span.
driver.findElement(By.xpath("//span[text()='text']").click();
Or if part of ID remain common, then you can use
//span[contains(#id,'act')]