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

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.

Related

Find xpath of element knowing a part of href attribute only selenium ide ui vision katalon recorder

I need to detect via xpath an element (image with href) but i know a part of href only
This is the html
<img src="//user/banners/16/08/1614708.gif" alt="AAA" data-tip="BBB" currentitem="false" class="" width="468" height="60">
I know the id 123456 (part of href)
I tried this xpath that recognize element with a part of href but working in text link only
xpath=//a[contains(#href, "123456")]
How can i detect the element using a part of href only ?
I need xpath only please.
You need get /img in //a where href attribute contains() or ends-with() your id
This is XPATH that you need. At least i would use this XPATH in this situation
//a[ends-with(#href, 'your-id-here')]/img
You can use regular expresions. Something like
driver.find_element_by_xpath("//input[starts-with (#name,'Tut')]")
or as you described
driver.find_element_by_xpath("//input[contains(#name,'sel')]").
Be awere of one thing, do not use double quotes, for the string you are searching as atribute value. Use a single quote like I previously described.

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!

Selenium FindElement by parent div

Trying to find link element of "a href". Snippet code:
<div id="contact-link">
Contact us
</div>
I managed doing it by:
Driver.FindElement(By.XPath("//*[#title='Contact Us']")).Click();
2.Driver.FindElement(By.XPath("//a[#href='http://automationpractice.com/index.php?controller=contact']")).Click();
3.Driver.FindElement(By.XPath("//*[text()='Contact us']")).Click();
Could someone tell me how can I get by firstly getting parent div and then find what's inside that div (by going from the top to the bottom)
So basically, with xpath, you are looking to replicate the HTML structure. What you need is:
//div[#id='contact-link']/a
This is going to return the a href under the div. Assuming its just 1, thats the way to go. If you want to go a little further, try:
//div[#id='contact-link']/a[#title='Contact Us']
Although you have already accepted the answer , I would like to highlight some point about Xpath and cssSelector. You should always pick cssSelector over Xpath :
Here is cssSelector for your requirement:
div[id='contact-link']>a
Code :
Driver.FindElement(By.CssSelector("div[id='contact-link']>a")).Click();
For more about cssSelector : https://www.w3schools.com/cssref/css_selectors.asp
For Difference between Xpath and cssSelector, you can read it from this SO post: Diff between Xpath and cssSelector

Is it okay to use such xpath to find web elements?

Consider this xpath which should always return one element.
//div[#id='MyDiv123']/div[contains(#class, 'super')]
Assume that we won't add anymore divs whose class is super. Given that info, I don't think that it is a good idea to use /div[contains(#class, 'super')]because the xpath will break if div[contains(#class, 'super')] is placed inside another element.
Shouldn't we be using //div[contains(#class, 'super')] instead ?
I don't like using XPaths for locators that can be written as a CSS selector. I think it's much simpler as
#MyDiv123 > div.super
or just
div.super
if it's unique on the page.
XPath contains() is a string match. All the elements below will match your XPath locator but none of them will match the CSS selectors above.
<div class="super-duper" ...>
<div class="superior" ...>
<div class="abcsuperdef" ...>
... you get the idea...
There is no defined Best Practices while writing xpaths. It all boils down to how effective xpath can be written.
I don't see any issue with the xpath as :
//div[#id='MyDiv123']/div[contains(#class, 'super')]
Of-coarse there ca be some improvements as follows :
As an enduser you won't be sure how the class attribute super impacts the HTML or which elements have this attribute. So in that case to identify the WebElement uniquely it would be wise to include the ancestor <div> tag with id as MyDiv123.
But it doesn't looks like the classname super can be dynamic. Hence you can avoid the keyword contains within the xpath and rewrite it as :
//div[#id='MyDiv123']/div[#class='super']

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')]