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 !
答案 0 :(得分:1)
您的定位器正在寻找“需求摘要”
"//*[normalize-space()='Demand Summary']"
但您提供的HTML显示“S / D摘要”
<span ...>S/D Summary</span>
将定位器更改为"//*[.='S/D Summary']"
,它应该可以正常工作。
答案 1 :(得分:1)
使用//*[normalize-space()='Demand Summary']
将无效,因为它会找到多个元素(整个主体,列表项,标记和span标记),但您的java代码正在寻找单个元素。请尝试使用//span[normalize-space()='Demand Summary']
答案 2 :(得分:0)
以下代码段应该能够找到元素:
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;']"));
详细的UI代码可能有助于编写更好,更小的定位器。
但是,您可以通过使用(对于aTagElement并且也可以扩展到span标记元素来减少选择器长度。)
style*='background: rgb(255, 255, 255)' // meaning style contains background color rgb(255, 255, 255)
或
style^='color: rgb(0, 0, 0)' //meaning style starts with color rgb(0, 0, 0)
或
style$='0%;' //meaning style ends with 0%.
答案 3 :(得分:0)
在您的代码中替换此行
driver.findElement(By.xpath("//*[normalize-space()='S/D Summary']"));
通过
driver.findElement(By.xpath("//li/a/span[text()='Demand Summary']"));
答案 4 :(得分:0)
感谢大家的宝贵时间和建议,但我通过以下代码解决了这个问题:
driver.findElement(By.linkText("Demand Summary")).click();