XPath- Getting Element from a table having dynamic ID - java

I'm trying to automate my Test Cases using Selenium for an OBIEE application. Now, I need to read a value from a tabular report generated. The problem is, the ID of the last cell where the total is, keeps on changing.
For example- Currently the id is: db_saw_9270_6_1610_0.
After refreshing, the ID becomes something else. The 4 numbers in between (9270) changes. The remaining bit are the same. I'm using the following logic to capture this element:
driver.findElement(By.xpath(".//*[contains(#id, '_6_1610_0')]")).getText();
But, it is returning org.openqa.selenium.NoSuchElementException: Unable to locate element:
Please tell me where did I go wrong and what should I do?

you can try starts-with and substring (as a substitute for xpath 2.0 methdod ends-with):
string xpath = "//*[starts-with(#id, 'db_saw_') and substring(#id, string-length(#id) - 8) = '_6_1610_0']"
driver.findElement(By.xpath(xpath)).getText();

You can try below xpath:-
driver.findElement(By.xpath("//*[starts-with(#id, 'db_saw')]")).getText();

driver.findElement(By.CSSselector("a[id*='_6_1610_0']")).getText();
Note: the a represents a html element. If your id is in a element then you have to replace a by table.
Check this out for more examples with css selector

Related

Parsing xml with multi childs using jsoup

I have an xml file that looks as follows - link.
I would like to get the title from it.
In order to do so, I did the following:
Document bookDoc = Jsoup.connect( url ).parser( Parser.xmlParser() ).get();
Node node = bookDoc.childNode( 2 ).childNode( 3 ).childNode( 3 );
This returns me this:
Now I have 2 questions:
Isnt there any simpler way to get this title instead of using all of these childNodes? My worry is that in some result the title wont exactly be at childNode(3) and all my code wont work.
How do I eventually get this title? Im stuck at this point and cant get the string of the title.
Thank you
You can use selectors to access elements. Here you want to select by tag name. Two ways to get the element you want:
String title1 = bookDoc.select("record>display>title").text();
String title2 = bookDoc.selectFirst("record").selectFirst("display").selectFirst("title").text();
If you want to select more complicated things read:
https://jsoup.org/cookbook/extracting-data/dom-navigation
https://jsoup.org/cookbook/extracting-data/selector-syntax
But you probably won't need them for parsing this XML.

How to locate a button with a dynamicID

I am automating an Android app using Appium where we need to click a button with a dynamic ID. Either the button has ID "PROFILEBUTTON" or ID "PROFILEMAILBUTTON". Apart from co-ordinates, what else can be used to automate clicking this button?
You can use partial id
driver.findElement(By.cssSelector("[id*='PROFIL'][id*='BUTTON']"));
Or with xpath
driver.findElement(By.cssSelector("//*[contains(#id, 'PROFIL') and contains(#id, 'BUTTON')]"));
driver.findElement(By.cssSelector("//*[contains(#id, 'PROFIL')][contains(#id, 'BUTTON')]"));
To identify an element with dynamic ID either PROFILEBUTTON or PROFILEMAILBUTTON you can use cssSelector with the following wildcards :
^ : To indicate an attribute value starts with
$ : To indicate an attribute value ends with
So the most granular locator would include the strategy to lookout for the initial letters i.e. PROFILE and the ending letters i.e. BUTTON and should be :
driver.findElement(By.cssSelector("[id^='PROFILE'][id$='BUTTON']"));
Update
As per your comment update, you can use either of the equivalent xpath as follows :
driver.findElement(By.xpath("//*[contains(#resource-id,'profileMail') and contains(#resource-id,'Button')]"));
//or
driver.findElement(By.xpath("//*[contains(#resource-id,'profileMailButton') or contains(#resource-id,'profileMailPremiumButton')]"));
driver.findElement(By.xpath("//*[contains(#resource-id,'profileMailButton') or contains(#resource-id,'profileMailPremiumButton')]"));
This worked for me.

findElement that is generated HTML during runtime

I'm trying to find an element that is generated from the PCA Predict API, found in this link here. http://www.pcapredict.com/en-gb/address-capture-software/
The code I have at the moment is as follows but it throws an timeout exception due to it not finding any elements. Yet the xpath is correct as I have checked it in developer tools.
By PCA = By.id("inputPCAnywhere");
driver.findElement(PCA).clear();
driver.findElement(PCA).sendKeys(ValidPostcode);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='pcaitem pcafirstitem']")));
driver.findElement(By.xpath("//div[#class='pcaitem pcafirstitem']")).click();
The element is visible on the page, and developer tools only returns one result that that xpath, there is no ID's to find it by.
It looks like that the first item is getting "selected" by default leading to it's class value being equal to the following:
<div class="pcaitem pcafirstitem pcaselected"...>...</div>
All other following results have only pcaitem class, but none have a pcaitem pcafirstitem class value.
In other words, your problem is the strict class match. I would improve the locator to have a partial match on the class attribute. For instance, with a CSS selector:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".pcaitem.pcafirstitem")));

Webdriver unable to hit submit button due no such element found error

Webdriver unable to hit submit button due no such element found error. Below is the code and error shown in the console while running the script.
public void passwordmatch() {
driver.findElement(By.id("encrypted_pwd")).sendKeys(pwd);
driver.findElement(By.id("confirm_pwd")).sendKeys(confirm_pwd);
driver.findElement(By.xpath("//*[#id='submit-btn']//*[#type='image']")).click();
if(pwd ==confirm_pwd) {
System.out.println("Password Match");
} else {
System.out.println("Password doesn't Match");
}
}
Error message is :
org.openqa.selenium.NoSuchElementException: Unable to locate element:
{"method":"xpath","selector":"//*[#id='submit-btn']//*[#type='image']"}
Command duration or timeout: 30.04 seconds
Given the following HMTL for the submit button:
<input src="/images/application/modules/default/submit-btn.jpg" class="submit-btn" type="image">
The reason you are receiving a NoSuchElementException when using //*[#id='submit-btn']//*[#type='image'] as a locator is firstly because the first part of the XPath - //*[#id='submit-btn'] is looking for any element in the page whose id attribute is equal to submit-btn, whereas the required element has a class attribute equal to submit-btn.
The second part of the XPath - //*[#type='image'] is looking for a child element with a type attribute equal to 'image' however the required element does not have any children.
Try using the following code in the offending line and let me know if it works:
driver.findElement(By.className("submit-btn")).click();
On an unrelated note, the line where you are trying to compare the passwords - if(pwd ==confirm_pwd) { is likely to be incorrect as you are comparing whether the two strings are pointing to the same String object.
You should instead use the .equals() method in a manner similar to:
if (pdw.equals(confirm_pwd)) {
Hi try doing the following.
driver.findElement(By.id("confirm_pwd")).submit();
above code will submit the form in which element with id 'confirm_pwd' is present.
i am sure this will solve your issue.
Try removing everything after the first ]. Then give it a go.
As in:
driver.findElement(By.xpath("//*[#id='submit-btn']")).click();

Problems with element locating with XPath in Selenium-RC

I'm trying to perform very simple automated test. I created XPath selector in a FirePath, here it is:
//a[#href='http://i.yandex.ru/'][span[contains(.,'ledak.e.v#yandex.by')]]
But Selenium-RC can't locate this element. Code is:
final String StrEmailToTest = "ledak.e.v#yandex.by";
String linkEmailSelector = "//a[#href='http://i.yandex.ru/'][span[contains(.,'"+ StrEmailToTest + "')]]";
selenium.isElementPresent(linkEmailSelector);
and it returns "false"
Could you tell me, what am I doing wrong?
UPD. I've uploaded the *.maft - file here: http://depositfiles.com/files/lhcdh2wtl
Don't be afraid, there are some russian characters on the screen.
Shouldn't your XPath be:
"//a[#href='http://i.yandex.ru/']/span[contains(.,'"+ StrEmailToTest + "')]";
My guess is that selenium is looking for the element even before it's loaded. Is it a dynamically loaded/generated element? If so, use waitForElementPresent(). If not, try changing the method of element identification - use id or name and then try to execute it. To make sure your xpath is correct, in the selenium IDE/plugin for firefox, type the path of the element(issue some random command for command field) and click on "Find Element". If it finds, then selenium has no problem finding it, given that the page/element is loaded or generated. If not, you will have to ask Selenium to wait till the element is loaded.

Categories

Resources