Reputation: 2157
I want to capture element and navigate on the page linked to that element which is is under <a>
tag and <span>
tag. I tried using selenium IDE but it is not able to capture those elements. So I wrote code in java and referred many links of stackoverflow regarding the same but still I am not able to solve my problem.
Below is my java code :
package com.selenium;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.websocket.api.Session;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FirstTestSelenium {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
WebDriver driver;
System.setProperty("webdriver.firefox.marionette", "D:\\geckodriver.exe");
driver = new FirefoxDriver();
Session session;
String baseUrl = "";
String expectedTitle = "QlikView";
String actualTitle = "";
// launch Fire fox and direct it to the Base URL
driver.get(baseUrl);
actualTitle = driver.getTitle();
System.out.println(actualTitle);
if (actualTitle.contentEquals(expectedTitle)) {
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
// driver.findElement(By.xpath("//span[text()='Demand
// Summary']")).getText();
// close Fire fox
driver.findElement(By.xpath("//*[normalize-space()='S/D Summary']"));
;
driver.close();
// exit the program explicitly
System.exit(0);
}
}
and below is my UI code :
<li style="display: list-item;" order="2" rel="DocumentSH07" id="Document\SH07">
<a style="color: rgb(0, 0, 0); background: rgb(255, 255, 255) none repeat scroll 0% 0%;" href="javascript:;">
<span style="font-weight: normal; font-family: Arial; font-size: 9pt; font-style: normal; text-decoration: none;">Demand Summary</span>
</a>
</li>
Please help me to solve my problem since I am new to selenium but trying my best. Its giving NoSuchElementFound Exception.
Thanks !
Upvotes: 0
Views: 1211
Reputation: 2157
Thank you everyone for your valued time and suggestions but I solved it by below code:
driver.findElement(By.linkText("Demand Summary")).click();
Upvotes: 0
Reputation: 1323
In your code replace this line
driver.findElement(By.xpath("//*[normalize-space()='S/D Summary']"));
by
driver.findElement(By.xpath("//li/a/span[text()='Demand Summary']"));
Upvotes: 0
Reputation: 1113
Using //*[normalize-space()='Demand Summary']
will not work, as this finds multiple elements (The whole body, the list item, the a tag and the span tag), but your java code is looking for a single element. Try //span[normalize-space()='Demand Summary']
instead
Upvotes: 1
Reputation: 25597
You locator is looking for "Demand Summary"
"//*[normalize-space()='Demand Summary']"
but the HTML you provided shows "S/D Summary"
<span ...>S/D Summary</span>
Change the locator to "//*[.='S/D Summary']"
and it should work.
Upvotes: 1
Reputation: 234
Following code snippet should be able to locate the element:
WebElement aTagElement = driver.findElement(By.cssSelector("a[style='color: rgb(0, 0, 0); background: rgb(255, 255, 255) none repeat scroll 0% 0%;']"));
WebElement spanTagElement = driver.findElement(By.cssSelector("span[style='font-weight: normal; font-family: Arial; font-size: 9pt; font-style: normal; text-decoration: none;']"));
A detailed UI code might help write better and smaller locator.
However you can reduce the selector length by using (for aTagElement and can be extended to the span tag element as well.)
style*='background: rgb(255, 255, 255)' // meaning style contains background color rgb(255, 255, 255)
or
style^='color: rgb(0, 0, 0)' //meaning style starts with color rgb(0, 0, 0)
or
style$='0%;' //meaning style ends with 0%.
Upvotes: 0