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"
Related
I am wondering if below is achievable using xpath
Given:
<label for="pt1:sc">Select Country</label>
<select id="pt1:sc">....</select>
Requirement:
I want to find select element using single xpath expression like below,
bcs ids are dynamic and always available in attribute 'for'.
//label[text()='Select Country']/#for//*[#id=#for]
Can we pass attribute value(here for attribute of label) in xpath, further down the path to find element.
Please do not suggest alternative using siblings, child, id or selenium get-attribute etc.
Thanks,
You can use something like this to select an element with an attribute value which refers to another attribute located in another element :
//*[#id=//label[text()='Select Country']/#for]
I'm not sure how it's going to work with your actual html, but it works on the example in the question:
//label[text()='Select Country'][#for=//select/#id]
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.
Can you please suggest me how can I set value
<input type="text" name="loginAccountName" maxlength="100" value="" id="loginAccountName" class="ui-input-text ui-body-c">
I have tried using CSS
driver.findElement(By.cssSelector("input[class=textbox ui-input-text ui-body-c]")).sendKeys("qwqweq");
But it is not working.
To locate the element, you have tried:
By.cssSelector("input[class=textbox ui-input-text ui-body-c]")
While working with cssSelector we have a much convinient way to specify them. Additionally, you need to drop the textbox class as it is not a part of the node attributes. Your effective cssSelector could have been:
By.cssSelector("input.ui-input-text.ui-body-c")
But the above mentioned cssSelector may not identify an unique element. Instead we will use the id or the name locator which remains unique through out the DOM tree as follows:
Using id:
By.cssSelector("input#loginAccountName").sendKeys("qwqweq");
Using name:
By.cssSelector("input[name=loginAccountName]").sendKeys("qwqweq");
For the below code
<li class="user ui-menu-item" role="presentation">
<a id="ui-id-52" class="ui-corner-all" tabindex="-1">
<em>User:</em>
Staff User
</a>
</li>
This is the scenario:
There is a text field, in which I enter the name Staff, with that the related values for the staff are displayed in the dynamic list box, in the above scenario the id is dynamically generated, and when I tried to select the value by class, it is same for all the elements.
I want an xpath expression to select the first available options in the list. I tried in many ways like with contains and starts-with, but no use. Please let me know your valuable suggestion.
Thanks in Advance
Shiva.
I think this should work
WebElement ele = webdriver.findElement(By.Xpath("//li[#class='user ui-menu-item'][1]"))
Did you try forming a List and then just directly using the first element of that List?
List<WebElement> list = driver.findElements(By.xpath("//li[#class(contains, 'user')]";
list[0].getText();
I'm still not 100% percentage sure what you want to do with the element once you have found it though. It would probably be better to form the List, then iterate over that list and perform whatever action you require per element.
Use this xpath
"(.//a[#class='ui-corner-all'])[1]"
source: XPath query to get nth instance of an element
Use the xpath. It should work
//li[#role = 'presentation']//a[1]
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()