Selenium无法找到特定的字段

时间:2013-06-27 15:53:44

标签: java selenium

我正在使用selenium在表单上提交数据。该网站最近更新了其表单,现在selenium找不到它在更改之前能够找到的字段。

我将数据提交到:http://bit.ly/ioKvAL

这就是我所拥有的:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.chrome.ChromeDriver;
//import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import java.util.List;

public class TwTest {

    public static void main(String[] args)
    {
        WebDriver driver = new FirefoxDriver();
        driver.get("SEE bit.ly LINK ABOVE");

        WebElement radio = driver.findElement(By.id("represent"));

        radio.click();

        WebElement box1 = driver.findElement(By.id("inf_tweet_button"));
        box1.click();

        WebElement radio1 = driver.findElement(By.id("tweet_website"));

        radio1.click();

        List<WebElement> radioOptions = driver.findElements(By.id("tweet_website_direct_1"));

        for(WebElement radioSelect : radioOptions)
        {
            if(radioSelect.getAttribute("value").equals("0"))
            {
                radioSelect.click();
                System.out.println(radioSelect.getAttribute("value"));
            }

        }

        WebElement more = driver.findElement(By.id("more_tweet_link"));
        more.click();

        WebElement acc2 = driver.findElement(By.id("tweet_url_2"));

        acc2.sendKeys("test");
    }
}

以上结果是

0
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"tweet_url_2"}
Command duration or timeout: 30 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.32.0', revision: '6c40c18', time: '2013-04-09 17:23:22'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_09'
Session ID: b0bea70f-7a0d-4a22-a951-864c0f6fe9ed
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=22.0}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:187)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:348)
    at org.openqa.selenium.By$ById.findElement(By.java:216)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)
    at TwTest.main(TwTest.java:43)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"id","selector":"tweet_url_2"}
Build info: version: '2.32.0', revision: '6c40c18', time: '2013-04-09 17:23:22'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_09'
Driver info: driver.version: unknown
    at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/USERCOMP/AppData/Local/Temp/anonymous2932086114655114107webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js:8405)
    at <anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/Users/USERCOMP/AppData/Local/Temp/anonymous2932086114655114107webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js:8414)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/USERCOMP/AppData/Local/Temp/anonymous2932086114655114107webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10421)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/USERCOMP/AppData/Local/Temp/anonymous2932086114655114107webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10426)
    at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/USERCOMP/AppData/Local/Temp/anonymous2932086114655114107webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10366)

1 个答案:

答案 0 :(得分:1)

您需要实施waitfluentwait。点击

WebElement more = driver.findElement(By.id("more_tweet_link"));
more.click();

webdriver尝试立即与webelement(.id("tweet_url_2"))交互(查看错误消息)

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"tweet_url_2"}
Command duration or timeout: 30 milliseconds # Fails in 30 milliseconds

失败的原因是,在.id("tweet_url_2")被点击之前,我们不存在we more_tweet_link