I have multiple ok buttons in my application in the combinations: OK, ok, oK and Ok. How can i write a single #findby expression to identify all of them with the one webelement.
Code example
<button type="button">OK</button>
Here is the other solution in pure xpath 1.0 using xpath functions.
//button[contains('OK,ok,Ok,oK',text())][string-length('OK')=2]
or
//button[contains('OK,ok,Ok,oK',text())][string-length(text())=string-length('OK')]
Edit: Simple approach using translate
//button[translate(text(),'ok','OK')='OK']
You can specify matching text with xpath:
//button[text()='OK']
In your case, to match them all:
//button[text()='OK' or text()='oK' or text()='ok' or text()='Ok']
Related
I am trying to click an element with text "No, Thanks" using below code, I tried various options so far such as
driver.findElementByXPath("//*[contains(text(),'THANKS')]").click();
driver.findElement(By.name("No,THANKS")).click();
driver.findElementByName("No,THANKS").click();
There is no other element with same text. I am using Appium Driver and Samsung device.
If it is TextView, you can consider the following
driver.find_element_by_xpath("//android.widget.TextView[#text='No, Thanks']")
Seems you were close. The text No, Thanks contains a , character in between which you need to avoid. So effectively you can use either of the following xpath based Locator Strategies:
xpath 1:
driver.findElementByXPath("//*[starts-with(., 'No') and contains(., 'Thanks')]").click();
xpath 2:
driver.findElementByXPath("//*[contains(., 'No') and contains(., 'Thanks')]").click();
Please try this:
driver.findElement(By.xpath("//android.widget.TextView[contains(#text,'No, Thanks')")).click();
I would like to know how to write the Xpath for validate the text -'Confirm Passowrd*' where there is more than 10 space gap between two words
When I tried to get it using chropath tool it gives Xpath like
//label[contains(text(),'Confirm Password*')]
But even that is also not working.
you can use the normalize-space in xpath.
//label[#ng-if='add_user' and normalize-space(text())='Confirm Password*']
You can also get text by:
driver.findElement(By.xpath("//label[#ng-if='add_user']")).GetAttribute("innerText");
OR
driver.findElement(By.xpath("//label[#ng-if='add_user']")).GetAttribute("value");
driver.findElement(By.xpath("//label").GetAttribute("value");
I am using XPath to find exact value:
//h5[#class='familyName productFamilyName'][contains(text(),'Dozers ')]
but it was failing because in my application there are 2 elements with text values "Dozers " and "Dozers wheel" which is come under same class.
I can't use id locators,because it is dynamically generating in my application like //div[#id="482"]/div/div[1]/h5.
Please suggest me any solution.
If you want to match element with exact innerHTML value just use
//h5[#class='familyName productFamilyName'][text()='Dozers')]
or
//h5[#class='familyName productFamilyName'][text()='Dozers wheel')]
Depending on HTML structure you might need to use [.='Dozers'] or
[normalize-space(.)='Dozers'] instead of [text()='Dozers']
How to select the checkbox which has a dynamically changing ID and XPath?
Multiple ways:
You should look at a pattern like id or name somoething like
CT_CHKBox_157, CT_CHK_158 etc.. For example, to click the first
Checkbox having a pattern of Ids
You can use a dynamic xpath like driver.findelement(By.xpath(//input[starts-with(#id,'CT_CHK'][1]).click()
Identify the Unique Element which are close ancestors to the
Checkbox in question and reach out to it through xpath or css path
relatively or through indexing from within.
Hope that clarifies.
Have you tried XPath by position? Ultimately the check boxes are like buttons or link that can be clicked so driver.findElement(By.xpath("//xpath by position")).click();
Alternativey you might want to use JavaScript:
((JavascriptExecutor) driver).executeScript("return document.getElementsByName('ChkboxValue')[0].checked;");
Hope this helps.
Selenium uses what is called locators to find and match the elements.There are 8 locators strategies included in Selenium:
Identifier
Id
Name
Link
DOM
XPath
CSS
UI-element
you can try using any other Locator in the list.
I am getting different results for the same locator. For example
//table[#id='foo']
returns true when testing ElementPresent, but returns 0 for XpathCount. In Selenium v1.0.10 IDE the Find button highlights the correct element for both functions. Any ideas on what could be causing this?
Notes:
We have frames on the page EDIT: This is probably the problem. Bounty to verification.
There are many tables on the page, but only one with #id of "foo"
Firefox 3.6
Happens in both IDE and Java RC
Well, this is not a verification more of a non-verification.
I use Selenium to test a GUI with frames. To make isElementPresent and getXpathCount to work I always have to select a frame first with selectFrame (even to get isElementPresent to work correctly). By just opening an URL no frame at all seems to be selected.
This is what the HTML and corresponding selectFrame code looks like:
<frameset id="mainframeset"><frame name="nav" id="nav" src....
selenium.selectFrame("nav");
Use these XPath expressions:
boolean(//table[#id='foo'])
and
count(//table[#id='foo'])
In case there is a table element whose id attribute's value is "foo", then the first expression above should evalute to true() and the second expression above should evalute to a positive integer.
Not really a direct answer to the question, but a workaround if you are reading this and want to loop over the elements. Use isElementPresent in the for loop like this:
for(int i = 2; selenium.isElementPresent("//table[#id='foo']//tr["+i+"]"); i++)
{
selenium.getText("//table[#id='foo']//tr["+i+"]//td["+columnNum+"]");
}
Note that we start i at 2 since XPath is indexed from 1 and we want to skip the header