【问题标题】:HtmlUnitDriver get mobile version (selenium framework)HtmlUnitDriver 获取手机版(selenium 框架)
【发布时间】:2015-06-28 12:41:14
【问题描述】:

这几天我尝试使用 Selenium 框架。这个框架有两个问题。

  • 在少数情况下,我会出现以下错误:“org.openqa.selenium.ElementNotVisibleException: You may only interact with visible elements”

但是我的元素是完全可见的,并且我在此页面上只有一个具有此名称的元素。当我更改浏览器版本时,可以避免这个错误,但它很烦人......

  • 在少数情况下,我获得了网站的移动版本,但没有找到任何解决方案。我怎样才能避免这种情况?

这是我在 twitter 上的代码示例:

    public static void main(String[] args) {

            HtmlUnitDriver htmlUnit = new HtmlUnitDriver(DesiredCapabilities.firefox());

            WebDriver driver = htmlUnit;

            goToTwitter(driver);

        }

public static void goToTwitter(WebDriver driver){

        driver.get("http://www.twitter.fr/login");

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("session[username_or_email]"));
        WebElement element2 = driver.findElement(By.name("session[password]"));

        // Enter something to search for
        element.sendKeys("****");
        element2.sendKeys("****");
        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
        System.out.println("Page title is: " + driver.getCurrentUrl());
    }

GetCurrentUrl 返回“mobile.twitter.com”,我需要这个站点的桌面版本。

所以我的问题是我们如何强制网站使用此框架返回桌面版本?

【问题讨论】:

    标签: java selenium mobile browser desktop


    【解决方案1】:

    我解决了这个问题,激活两种形式的 javascript,在 selenium 中使用方法 setJavascriptEnabled(true);并在 create newWebClient 这样,直接在 htmlunit 中设置 javascript

    public HtmlUnitCustomizedWebDriver(BrowserVersion browser) {
        super(browser);
        this.setJavascriptEnabled(true);//this line
    
    }
    ....
    @Override
    protected WebClient newWebClient(BrowserVersion version) {
        WebClient webClient = new WebClient(version);
        webClient.getCookieManager().setCookiesEnabled(true);
        webClient.getOptions().setTimeout(60000);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(true);//this line
        webClient.getOptions().setUseInsecureSSL(true);
        webClient.getOptions().setPopupBlockerEnabled(false);
    
        return webClient;
    }
    

    【讨论】:

    • 所以这个,我扩展 HtmlUnitDriver
    猜你喜欢
    • 2017-12-07
    • 1970-01-01
    • 2023-03-09
    • 2017-03-17
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    • 2023-03-29
    • 2020-12-07
    相关资源
    最近更新 更多