AngularJs page issue with selecting an element and clicking it - java

I have a problem with selecting and clicking an element it so the drop down occurs here is what i have tried uptill now:-
String csspath = "html body.ng-scope f:view form#wdesk.ng-pristine.ng-valid div.container div.ng-scope md-content.md-padding._md md-tabs.ng-isolate-scope.md-dynamic-height md-tabs-content-wrapper._md md-tab-content#tab-content-7._md.ng-scope.md-active.md-no-scroll div.ng-scope.ng-isolate-scope ng-include.ng-scope div.ng-scope accordion div.accordion div.accordion-group.ng-isolate-scope div.accordion-heading a.accordion-toggle.ng-binding span.ng-scope b.ng-binding";
String uxpath = "//html//body//f:view//form//div//div[2]//md-content//md-tabs//md-tabs-content-wrapper//md-tab-content[1]//div//ng-include//div//accordion//div//div[1]//div[1]//a";
String xpath2 = "/html/body/pre/span[202]/a";
xpath = "/html/body/f:view/form/div/div[2]/md-content/md-tabs/md-tabs-content-wrapper/md-tab-content[1]/div/ng-include/div/accordion/div/div[1]/div[1]/a/span/b";
try {
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(csspath)));
locator = By.cssSelector(csspath);
driver.findElement(locator).click();
} catch (Exception e) {
System.out.println("Not foune csspath");
}
try {
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
locator = By.xpath(xpath);
driver.findElement(locator).click();
} catch (Exception e) {
System.out.println("Not foune xpath");
}
try {
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(uxpath)));
locator = By.xpath(uxpath);
driver.findElement(locator).click();
} catch (Exception e) {
System.out.println("Not foune uxpath");
}
try {
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath2)));
locator = By.xpath(xpath2);
driver.findElement(locator).click();
} catch (Exception e) {
System.out.println("Not foune xpath2");
}
However nothing has worked till now i want to select responsibility code and give it values
It would be really appreciated if you can give me any insight
Thanks in advance
Here is a screenshot of my issue
enter image description here

First issue (as already pointed out in comments) is the absolute selectors you are using. For example, try to refactor your xpath selectors and make those relative.
Next issue is related to the
AngularJs page
itself. Let's look at Protractor, the testing framework for Angular built upon WebDriverJS, it provides additional WebDriver-like functionality to test Angular based websites. Put simple - your code needs extra functionality that will know when Angular elements are available for interaction.
Here is how to port some of the most useful Protractor functions to Java (and Python):

Related

Determine the nesting of pages on the site

It is necessary to determine the level of nesting of pages in clicks from home page. How to do it right? I understand that all the pages from the site will get recursively.
The code will look like this:
public void getPageLinks(String URL) {
//4. Check if you have already crawled the URLs
//(we are intentionally not checking for duplicate content in this example)
if (!links.contains(URL)) {
try {
//4. (i) If not add it to the index
if (links.add(URL)) {
System.out.println(URL);
}
//2. Fetch the HTML code
Document document = Jsoup.connect(URL).get();
//3. Parse the HTML to extract links to other URLs
Elements linksOnPage = document.select("a[href]");
//5. For each extracted URL... go back to Step 4.
for (Element page : linksOnPage) {
getPageLinks(page.attr("abs:href"));
}
} catch (IOException e) {
System.err.println("For '" + URL + "': " + e.getMessage());
}
}
}
Only still there will be a check whether the link is a link to an external site, if so then you do not need to go to it.

How to select item in a list from search Result page using Selenium in Java?

am trying to click on this game to open it's page but every time gives me null pointer exception whatever locator am using still gives me same error also i tried to do a select from list as the link seems to be inside an "li" but didnt work also.
anyone could help me with the code to click this item ??
Targeted Page Url:
https://staging-kw.games.getmo.com:/game/43321031
Search(testCase);
WebElement ResultList = driver.findElement(By.xpath(testData.getParam("ResultList")));
log.info("list located ..");
List<WebElement> Results = ResultList.findElements(By.tagName(testData.getParam("ResultListItems")));
for (WebElement List : Results) {
String Link = List.getAttribute("href");
try {
if (Link.equals(null)) {
log.info("null");
}
if (Link.equals(testData.getParam("GameLink")) && !Link.equals("") && !Link.equals(null)) {
List.click();
}
} catch (NullPointerException countryIsNull) {
log.info("LinkIsNull");
}
}
//clickLink(GameLocator,driver);
}`
I got this code to work by just adding this line after the search method
driver.findElement(By.cssSelector("li.title")).click();
and how did i get it ? .. i used Selenium IDE to record the actions then converted the code to Java -TestNG to get the exact web element selector

Getting All YouTube Video Ids/Urls From Video Manager

I'm using Selenium to log into my Google account and to visit YouTube.
Now on the video manager I would like to get all of my video ids. I tried copying the CSSSelector or XPath which the developer tools in Chrome give me but each of them contain the video id which makes them impossible to use like this:
List<WebElement> allVideoUrls = driver.findElements(By.cssSelector("my-selector-which-gives-all-videos-on-page"));
Note that I have to be logged in to be able to "see" unlisted or private videos as well so that's required.
So far I have a bad implementation which sometimes fails to work for some reason. I firstly get all the links on the page and only return the ones which are for editing a video. To avoid a StaleElementReferenceException I'm retrieving all links again inside the loop.
public void getVideoInformation()
{
// Visit video manager
driver.get("https://www.youtube.com/my_videos?o=U");
// Wait until video list has loaded
new WebDriverWait(driver, 10).until(ExpectedConditions
.visibilityOfElementLocated(By
.cssSelector("#vm-playlist-video-list-ol")));
// Return all links on page
List<WebElement> allLinks = driver.findElements(By.tagName("a"));
HashSet<String> videoLinks = new HashSet<>();
for (int linksIndex = 0; linksIndex < allLinks.size(); linksIndex++)
{
String link = driver.findElements(By.tagName("a")).get(linksIndex)
.getAttribute("href");
try
{
if (link.contains("edit"))
{
System.out.println(link);
// No duplicates
videoLinks.add(link);
}
} catch (Exception error)
{
error.printStackTrace();
}
}
// ...
}
I'm fine with the fact that I need to load every other page as well to get all the videos but please help me to find an efficient/reliable way of getting the video ids.

Cannot check the WebElement is Present or Not by "isDisplyed" on selenium WebDriver

I am trying to get the value from UI, using (By.id) locator.getAtrribute("value"),
Issue:
But, when the xpath is not found (web element not present on UI), I need to take the value as '00:00', but I get error as "No Such element"
I need to add a check, to find the webelement is present on UI, if true, get value using getattribute("value") , else return the value as '00:00'
But When using If condition, "(WebelementforUIvalue.isEmpty()" returns '00:00', though the Web element is present on UI. (I need to take 00:00, when there is no element found/present on UI)
String WebelementforUIvalue = driver.findElement(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket)).getAttribute("value");
if (WebelementforUIvalue.isEmpty()) {
UIValue = "00.00";
} else {
UIValue = WebelementforUIvalue;
}
System.out.println(UIValue);
Personally I'd use a try/catch block for this particular case.
try {
String WebelementforUIvalue = driver.findElement(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket)).getAttribute("value");
UIValue = WebelementforUIvalue;
}
catch (NoSuchElementException) {
UIValue ="00.00";
}
Try like this instead(Java Code). :
try{
WebElement element = new WebDriverWait(driver,20).until(ExpectedConditions.presenceOfElementLocated(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket));
UIValue = element.getAttribute("value");
}catch(Throwable e){
UIValue = "00.00";
}
System.out.println(UIValue);
I have given an explicit timeout of 20 seconds. So, selenium will try to detect the presence of element in 20 seconds. If it doesn't find then it will timeout and send the assign "UIValue" to "00.00" or else if it finds the element it will assign the "value" attribute's content to "UIValue".
you can try it out this way...
String WebelementforUIvalue = null;
try{
WebelementforUIvalue = driver.findElement(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket)).getAttribute("value");
} catch(NoSuchElementException nsee) {
WebelementforUIvalue = "0.0";
}
System.out.println(WebelementforUIvalue);
You can handle this in better way using "findElements"(observer 's' at the end).
findElements returns the list of elements matching the locating criteria.
List<WebElements> list=driver.findElements(By.id("Your criteria"))
Integer intNoValues=list.size();
if(intNoValues=0)
{
UIValue = "00.00";
}
else
{
UIValue = WebelementforUIvalue;
}
If you want a detailed explanation of findElements, you can watch the below video
How to find element which doesnt exist using WebDriver

java,Webdriver, I want to fetch all links in a webpage and then compare them to a string name that I have and if it is found then click it

java, Webdriver, So now i have this, but the casting in the third line is giving me errors
public void getLinks(String linkName)throws Exception{
try {
List<WebElement> links = ((Webelement)driver).findElements(By.tagName("a"));
for (WebElement myElement : links){
String link = myElement.getText();
if (link.equals(linkName)){
myElement.click();
}
}
}catch (Exception e){
System.out.println("Error the link was not found "+e);
}
}
and if i run it this is what i see: Starting ChromeDriver (v2.6.232923) on port 34733
Error the link was not found java.lang.ClassCastException: java.lang.ThreadLocal cannot be cast to org.openqa.selenium.WebElement
PASSED: testing
Also my second idea is that it is not finding: “tagName("a"));” for links. But then when I go to the web I see Contacts in Portugal covering Airlines
So the links are in “a” tags, so I don’t think is this.
And im passing this : String linkName= "Contacts in Portugal covering Airlines";
I think this is a wrong assertion:
if (assertEquals(linkName, myElement)){
myElement.click();
}
.. because linkName is a String, myElement is a WebElement and the assertEquals() return value is not boolean (only void). You can check it with String equals(). For example:
if (link.equals(linkName)){
myElement.click();
}

Categories