Selenium java - HtmlUnitDriver查找元素

时间:2014-12-04 18:51:24

标签: java selenium

所以我正在编写一个自动执行网页操作的程序,而我正在使用FirefoxDriver来执行此操作,但现在我可以使用HtmlUnitDriver代替。 这段代码很好用:

                tempStr = AutoBot.queue.get(i);
                if(k == 0)
                {
                    System.out.println(ForumDialogBS.forumURL);
                    driver.get(ForumDialogBS.forumURL);
                    baseWinHandle = driver.getWindowHandle();
                    temp = AutoBot.logDetails.get("stuff");
                    element = driver.findElement(By.className("login-window"));
                    element.click();
                    element = driver.findElement(By.name("vb_login_username"));
                    element.sendKeys(temp.getUser());
                    element = driver.findElement(By.id("navbar_password_hint"));
                    element.sendKeys(temp.getPswd());
                    element = driver.findElement(By.className("loginbutton"));
                    element.click();
                    temp.rst();
                    driver.switchTo().window(baseWinHandle);
                    k = 1;
                    pause(2);
                }
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("qr_preview")));
                element = driver.findElement(By.id("qr_preview"));
                element.click();
                element = driver.findElement(By.cssSelector("textarea.cke_source"));
                element.sendKeys(tempStr);
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("sbutton")));
                element = driver.findElement(By.name("sbutton"));
                element.click();
                System.out.println((i+1) + "//" + AutoBot.toIndex);
                pause(8);

驱动程序是这样的:

String tempStr;
String baseWinHandle;

FirefoxDriver driver = new FirefoxDriver();

WebDriverWait wait = new WebDriverWait(driver, 60);

WebElement element;

List<WebElement> inputs;

但是当我尝试更改为HtmlUnitDriver时,我遇到了定位元素的问题。 首先我得到这样的错误:

:43:57 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error WARNING: CSS error: 'http://pornsavant.com/clientscript/vbulletin_css/style00011l/main-rollup.css?d=1415642704' [1:48457] Error in expression. (Invalid token ";". Was expecting one of: <S>, <NUMBER>, "inherit", <IDENT>, <STRING>, <PLUS>, <HASH>, <EMS>, <EXS>, <LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, <LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, <TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <PERCENTAGE>, <DIMENSION>, <URI>, <FUNCTION>, "-".)

然后:

    Exception in thread "Thread-5" org.openqa.selenium.TimeoutException: Timed out after 60 seconds waiting for visibility of element located by By.id: qr_preview 
Driver info: org.openqa.selenium.htmlunit.HtmlUnitDriver
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:79)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:228)
    at pl.posterhelper.Poster.run(Poster.java:135)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element with ID: qr_preview
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Driver info: driver.version: HtmlUnitDriver
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementById(HtmlUnitDriver.java:949)
    at org.openqa.selenium.By$ById.findElement(By.java:214)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1637)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1291)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1633)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:539)
    at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:730)
    at org.openqa.selenium.support.ui.ExpectedConditions.access$0(ExpectedConditions.java:728)
    at org.openqa.selenium.support.ui.ExpectedConditions$4.apply(ExpectedConditions.java:130)
    at org.openqa.selenium.support.ui.ExpectedConditions$4.apply(ExpectedConditions.java:1)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:208)

如果我删除&#34;等待&#34;我得到错误,说返回元素不是Html元素

是否有人可以帮助解决这些错误? 我到处搜索,无法找到答案。

0 个答案:

没有答案