Selenium-点击youtube链接

Selenium-点击youtube链接,selenium,xpath,selenium-webdriver,Selenium,Xpath,Selenium Webdriver,我刚刚尝试打开youtube链接的第一个搜索结果。 这是我的密码。因为youtube的结果在iFrame中,所以我使用了SwitchTo.frame()方法 运行此代码时,它返回为 Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element 在findElement中使用xpath。请告诉我我错过了什么。请把我看作硒的新手 我在年的Youtube页面上进行了调查,结果如下: dr

我刚刚尝试打开youtube链接的第一个搜索结果。 这是我的密码。因为youtube的结果在iFrame中,所以我使用了SwitchTo.frame()方法

运行此代码时,它返回为

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element 

在findElement中使用xpath。请告诉我我错过了什么。请把我看作硒的新手

我在年的Youtube页面上进行了调查,结果如下:

driver = new FirefoxDriver();
baseUrl = "http://www.youtube.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(baseUrl + "/results?search_query=selenium&sm=3");
driver.findElement(By.id("masthead-search-term")).clear();
driver.findElement(By.id("masthead-search-term")).sendKeys("selenium");
driver.findElement(By.cssSelector("span.yt-ui-ellipsis-wrapper")).click();
请注意最后一行:那是我点击第一个搜索结果的地方。在使用Web调查Youtube页面后,我意识到这里有一个
iframe
,但它只包含一些AdSense的跟踪代码(我猜)


一般注意事项:始终首先尝试使用Selenium IDE进行调查。它让我从关于selenium代码的许多噩梦中解脱出来;)

我不知道你为什么说结果在
中。。。我没有看到一个iframe。以下是使用该框架的脚本:

@Config(url="http://youtube.com")
public class YouTubeTest extends AutomationTest {
    @Test
    public void myTest() {
        setText(By.id("masthead-search-term"), "selenium tutorial for beginner")
        .click(By.id("search-btn"))

        // navigate to a search result based on index
        .click(By.cssSelector("ol#search-results > li:nth-child(X) a.yt-uix-tile-link"))
        ;
    }
}
单击(By.css选择器
上,您需要将
X
值替换为
1
2
,等等。无论您要单击的链接的索引是什么

(如果您不使用该框架,您可以轻松地翻译代码并提取我使用的选择器)

@Config(url="http://youtube.com")
public class YouTubeTest extends AutomationTest {
    @Test
    public void myTest() {
        setText(By.id("masthead-search-term"), "selenium tutorial for beginner")
        .click(By.id("search-btn"))

        // navigate to a search result based on index
        .click(By.cssSelector("ol#search-results > li:nth-child(X) a.yt-uix-tile-link"))
        ;
    }
}