use*_*466 2 java xpath webdriver selenium-webdriver
我想在网页上打开一个链接.该链接似乎位于标记内的无序列表中.网页的网址是selftechy dot com.标签是家,关于,硒.
我试图打开链接使用,driver.findElement(By.linkText("Selenium"));
但页面似乎失去了它的样式.我也尝试过使用xpath方法,但它也不起作用.请向我解释为什么它不起作用,我应该如何修改代码以使其正常工作.谢谢你的帮助.
HTML代码片段:
<body class="custom">
<div id="container">
<div id="page">
<ul class="menu">
<li class="tab tab-home current"><a href="http://selftechy.com">Home</a></li>
<li class="tab tab-1"><a href="http://selftechy.com/about" title="About">About</a></li>
<li class="tab tab-2"><a href="http://selftechy.com/selenium-2" title="Selenium">Selenium</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
webdriver代码打开链接
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import org.junit.Before;
import org.junit.After;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class selftechyTestng
{
private WebDriver driver;
private String baseUrl;
@Before
public void setUp() throws Exception
{
driver = new FirefoxDriver();
baseUrl = "http://selftechy.com/";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void searchElements() throws Exception{
driver.get(baseUrl);
//use By.linkText method the page lost its styling
driver.findElement(By.linkText("Selenium"));
//use xpath method to open the link doesn't work either
List<WebElement> elements = driver.findElements(By.xpath("//div[@id=page]/*[3]")).click();
driver.findElement(By.xpath("//div[@id=page]/*[3]")).click();
}
}
Run Code Online (Sandbox Code Playgroud)
为什么要搜索div然后搜索子元素 - 有什么特别的原因吗?我没有看到任何优势,当然你没有得到a
你真正想要点击的元素.在我看来,它使用起来要简单得多
driver.findElement(By.xpath("//a[@title = 'Selenium']")).click();
Run Code Online (Sandbox Code Playgroud)
使用您必须使用的方法
driver.findElement(By.xpath("//div[@id = 'page']/ul/li[3]/a")).click();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
32522 次 |
最近记录: |