SeleniumJava-HtmlUnitDriver查找元素

SeleniumJava-HtmlUnitDriver查找元素,java,selenium,Java,Selenium,因此,我正在编写一个程序,自动在网页上执行操作,我使用的是FirefoxDriver,但现在我可以真正使用HtmlUnitDriver。 这段代码工作得很好: tempStr = AutoBot.queue.get(i); if(k == 0) { System.out.println(ForumDialogBS.forumURL);

因此,我正在编写一个程序,自动在网页上执行操作,我使用的是
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;
: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)
如果我删除“wait”,我会得到一个错误,说返回的元素不是Html元素

有没有人可以帮助解决这些错误?
我到处都搜索过,找不到任何答案。

尝试在顶部使用40-50秒的隐式等待,并在指定的位置丢失所有显式等待。看看这是否有效。据我所知,异常与HtmlUnitDriversaly有关,设置隐式等待而不是显式等待根本没有任何帮助,我仍然会遇到这个错误:线程“thread-5”org.openqa.selenium.NoSuchElementException中的异常:返回的节点不是HTML元素我也遇到了与HTMLUnitDriver相同的错误。用chromedriver替换HTMLUnitDriver,一切正常@苏瑞尤森你能解决这个问题吗?