Selenium WebDriver CSS equivalent to xpath not contains - xpath

I want to avoid using xpath in my java selenium code but cannot figure out what the css equivalent to the code below would be
("xpath", "//div[#class='error'][not(contains(#style,'display: none'))]");
Does anyone know if there is a CSS equivalent to xpath not contains

You can't easily match against a compound attribute (i.e., style=) in CSS. The not part is easy - CSS3 has a :not(...) selector. You're going to have to find something else to identify the elements you want to exclude, and then use :not(...) to exclude them,

Related

Would like to use wildcard within an xpath

I have two links on a page that have the text 'London'. I want to choose the second one on the page, but I want to define the xpath in a way that it chooses by the parent div, but I want to use wildcard in case the link is moved.
So, the two xpaths are
//div[#id="first-id"]/div/div[2]/a[text()="London"]
//div[#id="second-id"]/div[2]/div[3]/div/a[text()="London"]
I want to use a wildcard and define the xpath within the parent div:
i.e. //div[#id="second-id"]/*/a[text="London"]
I already understand I can just use the full xpath and not have any wildcards, but I want to know if there's a way to do what I am proposing using xpath. I thought maybe contains() in some way would work but am not familiar enough with it.
To find the a element wherever it may appear within the div element, the descendant path is represented simply by //:
//div[#id="second-id"]//a[text="London"]

How to use Selenium using Xpath to determine the classes of an element?

I am trying to use xpath within selenium to select a div element that is within a td.
What I am really trying to do is determine the class of the div and if it is either classed LOGO1, LOGO2, LOGO3 and so on. Originally I was going to just snag the image:url to determine with logo.jpg was used but whoever made the target website used one image for each logo type and used css to determine which portion of the image will be displayed. So Imagine 4 images on one sprite image. This is the reason why I have to determine the class of the div instead of digging through the css paths.
In selenium I am using storeElementPresent | /html/body/form/center/table/tbody/tr/td[2]/div[3]/div[2]/fieldset/table/tbody/tr[2]/td/div/table/tbody/tr[${i}]/td[8]/div//class | cardLogo .
The div has multiple classes so I am thinking that this is the issue, but any help is appreciated. Below is the target source. This is source from within the table in the tbody. Selenium has no problems identifying all the way up to td[8] but then fails to gather the div. Please help!
<td class="togglehidefields" style="width:80px;">
<div class="cardlogo LOGO1" style="background-image:url(https://www.somesite.com/merchants/images/image.jpg)"></div>
<span id="ContentPlaceHolder1_grdCCChargebackDetail_lblCardNumber_0">7777</span>
</td>
I was fiddling with selenium.getAttribute() but it kept erroring out, any ideas there?
This <div/> element has one class attribute with one value, but this one is tokenized when parsed as HTML.
As selenium only supports XPath 1.0, you will need to check for classes like this:
//div[contains(#class, "LOGO1") or contains(#class, "LOGO2")]
Extend that pattern as needed and embed it in your expression.
With XPath 2.0 and better, you could tokenize and use the = operator which works on a set-based semantics:
//div[tokenize(#class, ' ') = ("LOGO1", "LOGO2")]
Old post but I'll put the solution I used up just in case it can help anyone.
xpath=//div[contains(#class,'carouselNavNext ')]/.[contains(#class, 'disabled')]
Fire of your contains, and then follow with /. to check children AND the current element.

I have xpath written in Selenium RC & now I want to write the same xpath in selenium Webdriver. below is the xpath

I have xpath written in Selenium RC & now I want to write the same xpath in selenium Webdriver. below is the xpath:
"//table[#id='lgnLogin']/tbody/tr/td/table/tbody/tr[4]/td"
by using this xpath, i am capturing the error message displayed on my application like "Please check your Password".
Now how can I write it in Webdriver. I have different ways but, not worked out.
"String msg= driver.findElement(By.xpath("//*[#td='error2']")).toString();" - This is what i did in Webdriver.
Please help me out on this...
The XPath hadn't change from Selenium RC to WebDriver.
If you were able to use the some XPath-expression before it should work in WebDriver too (in 99% of cases as usual).
The code below should work but it's pretty hard to answer without seeing your HTML.
String msg= driver.findElement(By.xpath("//table[#id='lgnLogin']/tbody/tr/td/table/tbody/tr[4]/td")).getText();
//*[#td='error2'] - this expression looks bad as it means "any element with attribute named "td" and value "error2". I suppose that you don't have 'td' attributes in your HTML.
And to the end. I wouldn't recommend to use such long XPath expressions as in your example. It might be broken due to any minor change in layout. It's better to use some specific attributes rather than long hierarchy.
A good way to start would be to start with the basics : http://seleniumhq.org/docs/03_webdriver.html#introducing-the-selenium-webdriver-api-by-example
What findElement would do is just locate the element that you want to interact with. After you locate the element you need to perform the action that you want to do with the element. In your case, you want the text inside the element. So you can look at appropriate functions of the WebElement interface and use the appropriate method, in your case, getText.

What is the difference between locator and selector?

When I'm writing test scripts for PHPUnit, I read a lot about selectors and locators, but I don't really know what's the difference.
Does anyone know the difference?
Selenium uses what is called locators to find and match the elements of your page that it needs to interact with. There are 8 locators strategies included in Selenium:
Identifier
Id
Name
Link
DOM
XPath
CSS
UI-element
Here the CSS locator strategy uses CSS selectors to find the elements in the page.What else is your doubt in these?

Brackets in XPath expressions in SeleniumIDE

I have a problem with SeleniumIDE's evaulation of XPath. SeleniumIDE seems to not know the round brackets in this xpath:
(//span[#class='section-name entry-box'])[last()]/input
It cannot find even this xpath:
(//span[#class='section-name entry-box'])[last()]
or
(//span[#class='section-name entry-box'])/input
But in FireFox FireBug console or in FireFox plugin XPather, all the xpaths works and finds one (or in the last example two) elements on the page.
Do you know any solution? I need to choose last element, which has specific class.
Put "xpath=" at the beginning. Selenium locators can be a variety of different types, and Selenium assumes the type is XPath if the locator starts with "//". Your's doesn't, so you need to specify it explicitly.

Resources