51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2179|回复: 4
打印 上一主题 下一主题

[菜鸟求助] Selenium无法点击百度搜索结果

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2016-1-4 17:09:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

package TestPackage;
import org.openqa.selenium.WebDriver.*;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.*;

public class Test2
{
        /**
         * @param args
         * @throws InterruptedException
         */
        public static void main(String[] args) throws InterruptedException
        {

                WebDriver driver=new FirefoxDriver();
                Navigation navigation = driver.navigate();
                navigation.to("http://www.baidu.com");
               
                WebElement BaiduTextbox = driver.findElement(By.id("kw"));
                BaiduTextbox.sendKeys("Hello");
                driver.findElement(By.id("su")).click();
                driver.findElement(By.xpath(".//*[@id='2']/h3/a/text()"));
       
        }
}


报错:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: The given selector .//*[@id='2']/h3/a/text() is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: The result of the xpath expression ".//*[@id='2']/h3/a/text()" is: [object Text]. It should be an element.
Command duration or timeout: 31 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
System info: host: 'SHNDTPDL0883', ip: '10.225.117.12', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_55'
*** Element info: {Using=xpath, value=.//*[@id='2']/h3/a/text()}
Session ID: f92e4f2b-43be-4fba-abfb-6ec3d6bea5f5
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=43.0.3}]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:353)
        at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:490)
        at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:345)
        at TestPackage.Test2.main(Test2.java:33)
Caused by: org.openqa.selenium.InvalidSelectorException: The given selector .//*[@id='2']/h3/a/text() is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: The result of the xpath expression ".//*[@id='2']/h3/a/text()" is: [object Text]. It should be an element.
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
System info: host: 'SHNDTPDL0883', ip: '10.225.117.12', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_55'
Driver info: driver.version: unknown
        at <anonymous class>.FirefoxDriver.annotateInvalidSelectorError_(file:///C:/Users/275873/AppData/Local/Temp/anonymous3656865153537910253webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10633)
        at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/275873/AppData/Local/Temp/anonymous3656865153537910253webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10664)
        at <anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/Users/275873/AppData/Local/Temp/anonymous3656865153537910253webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10668)
        at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/275873/AppData/Local/Temp/anonymous3656865153537910253webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12534)
        at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/275873/AppData/Local/Temp/anonymous3656865153537910253webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12539)
        at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/275873/AppData/Local/Temp/anonymous3656865153537910253webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12481)


感觉上好像是要点的URL没找到。。。求大神帮忙-。-


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

  • TA的每日心情
    开心
    2016-6-1 13:38
  • 签到天数: 8 天

    连续签到: 2 天

    [LV.3]测试连长

    2#
    发表于 2016-1-4 17:33:31 | 只看该作者
    ".//*[@id='2']/h3/a/text()" is: [object Text]. It should be an element. 这句话说得很明显了,你通过这种方式 driver.findElement(By.xpath(".//*[@id='2']/h3/a/text()")); 最终的结果是找到一个 元素,而不是元素的text值;你想要得到元素的值,是先要找到元素,然后用这个元素再去活动值;
    Element element= driver.findElement(By.xpath(".//*[@id='2']/h3/a/")); --这里先获取到元素 对象
    element.getText()--再用 获取到的元素对象调用getTest()方法去获取text值

    评分

    参与人数 1测试积点 +10 收起 理由
    lsekfe + 10 恭喜您获得测试积点10

    查看全部评分

    回复 支持 反对

    使用道具 举报

    该用户从未签到

    3#
     楼主| 发表于 2016-1-5 14:46:38 | 只看该作者
    还是有问题,HTML代码如下
    <h3 class="t c-gap-bottom-small">
    <a target="_blank" href="http://www.baidu.com/link?url=R7HJnKkfFHYA6Roqw2KpdaRoC_eVNeRh1BBUte-KvpURCZ0wBPt-DJ09p28aoCSEt9Em85oC7hj5zvl8YIyUU_B2nI7i4uKLiLrFgJBWpRm">
    <em>hello world</em>
    _百度百科
    </a>
    </h3>

    当执行        WebDriver driver=new FirefoxDriver();
                    driver.get("http://www.baidu.com");
                    driver.manage().window().maximize();
                    driver.findElement(By.id("kw")).clear();
                    driver.findElement(By.id("kw")).sendKeys("Hello world");
                    //Open Baidu and search Hello world

                    WebElement element = driver.findElement(By.cssSelector(".c-gap-bottom-small>a>em"));
                    System.out.println(element.getText());
    时,成功输出hello world
    但是当执行        WebDriver driver=new FirefoxDriver();
                    driver.get("http://www.baidu.com");
                    driver.manage().window().maximize();
                    driver.findElement(By.id("kw")).clear();
                    driver.findElement(By.id("kw")).sendKeys("Hello world");
                    //Open Baidu and search Hello world

                    //WebElement element = driver.findElement(By.cssSelector(".c-gap-bottom-small>a>em"));
                    //System.out.println(element.getText());
                   
                    driver.findElement(By.cssSelector(".c-gap-bottom-small>a>em"));
                    if (driver.findElement(By.cssSelector(".c-gap-bottom-small>a>em")) != null)
                    {System.out.println("ok");}
    时却找不到这个element了,报错如下:
    Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":".c-gap-bottom-small>a>em"}
    Command duration or timeout: 429 milliseconds
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
    Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
    System info: host: 'SHNDTPDL0883', ip: '10.225.117.12', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_55'
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    4#
     楼主| 发表于 2016-1-5 14:51:45 | 只看该作者
    页面HTML代码如下:
    div id="content_left">
    <div id="1" class="result-op c-container xpath-log" data-click="{'p1':'1','rsv_bdr':'0','fm':'albk',rsv_stl:'0'}" data-op="{'y':'E3FFF577'}" mu="http://baike.baidu.com/subview/47227/11216556.htm?fr=aladdin" tpl="bk_polysemy" fk="Hello world" srcid="91">
    <h3 class="t c-gap-bottom-small">
    <a target="_blank" href="http://www.baidu.com/link?url=R7HJnKkfFHYA6Roqw2KpdaRoC_eVNeRh1BBUte-KvpURCZ0wBPt-DJ09p28aoCSEt9Em85oC7hj5zvl8YIyUU_B2nI7i4uKLiLrFgJBWpRm">
    <em>hello world</em>
    _百度百科
    </a>
    </h3>
    <div class="c-row">
    <div class="c-span6">
    <div class="c-span18 c-span-last">
    </div>
    <div class="c-gap-top c-recommend" style="display:none;">
    <i class="c-icon c-icon-bear-circle c-gap-right-small"/>
    <span class="c-gray">为您推荐:</span>
    <a title="hello world c++" href="/s?rsv_idx=1&wd=hello+world+c%2B%2B&usm=2&ie=utf-8&rsv_cq=Hello+world&rsv_dl=0_left_bk_polysemy_91_19781" target="_blank">hello world c++</a>
    <a class="c-gap-left-large" title="hello world 血界战线" href="/s?rsv_idx=1&wd=hello+world+%E8%A1%80%E7%95%8C%E6%88%98%E7%BA%BF&usm=2&ie=utf-8&rsv_cq=Hello+world&rsv_dl=0_left_bk_polysemy_91_19781" target="_blank">hello world 血界战线</a>
    <a class="c-gap-left-large" title="hello world 程序" href="/s?rsv_idx=1&wd=hello+world+%E7%A8%8B%E5%BA%8F&usm=2&ie=utf-8&rsv_cq=Hello+world&rsv_dl=0_left_bk_polysemy_91_19781" target="_blank">hello world 程序</a>
    </div>
    </div>

    当执行
    WebElement element = driver.findElement(By.cssSelector(".c-gap-bottom-small>a>em"));
    System.out.println(element.getText());
    时,能够输入hello world
    但是当执行
    driver.findElement(By.cssSelector(".c-gap-bottom-small>a>em"));
    if (driver.findElement(By.cssSelector(".c-gap-bottom-small>a>em")) != null)
    {System.out.println("ok");}
    时,报错。。。如下:
    Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":".c-gap-bottom-small>a>em"}
    Command duration or timeout: 429 milliseconds
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
    Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
    System info: host: 'SHNDTPDL0883', ip: '10.225.117.12', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_55'

    现在的问题是,为什么我能够output Text,但是driver.findElement找出来的东西是空?
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    5#
    发表于 2016-1-5 17:07:22 | 只看该作者
    driver.findElement(By.xpath(".//*[@id='2']/h3/a/text()"));     改为:driver.findElement(By.xpath("//*[@id='2']/h3/a")).click();
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-11-27 14:22 , Processed in 0.069327 second(s), 26 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表