首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法使用带有Selenium/testNG的xpath节点获取yt频道名称

无法使用带有Selenium/testNG的xpath节点获取yt频道名称
EN

Stack Overflow用户
提问于 2013-10-11 23:03:44
回答 2查看 259关注 0票数 1

我的任务是打开浏览器,进入YT,输入搜索词"qtp",点击搜索按钮。在结果页面上,我想要获取频道名称。为此,我使用Firebug应用程序获取前3个频道名称的以下路径:

当我使用Selenium / testNG执行以下代码时,我捕捉到了异常,它显示“找不到元素”。我仅用Selenium (没有TestNG)尝试了类似的代码,仍然不能工作。尝试使用WebDriver,仍然不起作用。

我确实理解它可能与节点有一些问题,但我无法弄清楚。

使用的Jars : selenium-server-standalone-2.35.0.jar,junit-4.10.jar,selenium-java-2.35.0.jar,selenium-server-2.35.0.jar;

代码语言:javascript
运行
复制
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestCase;
import com.thoughtworks.selenium.SeleniumException;


public class YT1 extends SeleneseTestCase{
@BeforeTest
public void beforeTest(){
System.out.println("Before");
selenium = new DefaultSelenium("localhost", 4444,"*firefox","http://www.youtube.com"); 
    selenium.start();
}

@Test
public void myTest() throws Exception{
    System.out.println("Test");
    selenium.open("/");
    selenium.type("//*[@id='masthead-search-term']", "qtp");
    selenium.click("//*[@id='search-btn']");
    Thread.sleep(3000);
    try {
    System.out.println(selenium.getText("//html/body/div[2]/div[4]/div/div[3]/div/div/div[2]/div[2]/div[2]/div[2]/ol/li[1]/div[2]/div/ul/li/a"));
    }
    catch (SeleniumException se1){
        System.out.println("Element not Found because: " + se1);
    }
    try {
    System.out.println(selenium.getText("//html/body/div[2]/div[4]/div/div[3]/div/div/div[2]/div[2]/div[2]/div[2]/ol/li[2]/div[2]/div/ul/li/a"));
        }
        catch (SeleniumException se2){
            System.out.println("Element not Found because: " + se2);
        }
    try {
        System.out.println(selenium.getText("//html/body/div[2]/div[4]/div/div[3]/div/div/div[2]/div[2]/div[2]/div[2]/ol/li[3]/div[2]/div/ul/li/a"));
        }
        catch (SeleniumException se3){
            System.out.println("Element not Found because: " + se3);
    }
}
@AfterTest
public void ShutDown(){
    System.out.println("After Test");
    selenium.close();
}

}

EN

Stack Overflow用户

发布于 2013-10-12 01:23:41

@pault的解决方案的替代方案是使用CSS。It's cleaner, quicker, and more readable.

代码语言:javascript
运行
复制
div#results > ol#search-results > li > div.yt-lookup-content > div.yt-lookup-meta a.yt-user-name
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19321404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档