我的网站不会在centos7中通过带有java、selenium和chromedriver的brosermob\u代理加载

raogr8fs  于 2021-06-26  发布在  Java
关注(0)|答案(0)|浏览(296)

在我之前的[question][1]之后,当我认为我的测试不起作用时,因为某种原因selenium和java没有连接它们自己,几天前我意识到问题只出现在我在centos7 docker中使用browsermob戡proxy时(当我在windows中使用它时,在我开发测试的地方,它工作得很好)。
我有一个小例子,我希望有人能帮助我,我把“http\u proxy”环境变量和测试仍然没有工作。

package seleprueba;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import net.lightbody.bmp.BrowserMobProxyServer;
import net.lightbody.bmp.client.ClientUtil;
import net.lightbody.bmp.proxy.auth.AuthType;
import org.openqa.selenium.Proxy;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class SelePrueba {

public static void main(String[] args) throws IOException {
    try {
        String baseUrl = "https://stackoverflow.com/questions/65080690/exception-in-thread-main-org-openqa-selenium-nosuchframeexception-no-such-fra";
        String driverPath = "/usr/src/scripts/ext/chromedriver";
        String driverName = "webdriver.chrome.driver";
        System.setProperty(driverName, driverPath);
        BrowserMobProxyServer proxy = new BrowserMobProxyServer();

        proxy.chainedProxyAuthorization("USERPROXY", "PASSWORDPROXY", AuthType.BASIC);
        proxy.start(0);

        Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
        seleniumProxy.setHttpProxy("some.proxy.example:8080");
        seleniumProxy.setSslProxy("some.proxy.example:8080");

        ChromeDriver driver;

        ChromeOptions chromeOptions = new ChromeOptions().setHeadless(true);
        DesiredCapabilities seleniumCapabilities = new DesiredCapabilities();
        seleniumCapabilities.setCapability(CapabilityType.PROXY, seleniumProxy);

        chromeOptions.addArguments("no-sandbox");
        chromeOptions.addArguments("disable-dev-shm-usage");
        chromeOptions.addArguments("--proxy-server=http://some.proxy.example:8080");

        chromeOptions.merge(seleniumCapabilities);
        driver = new ChromeDriver(chromeOptions);

        driver.get(baseUrl);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

        Actions ac1 = new Actions(driver);
        WebElement wb1 = driver.findElementByXPath("/html/body/header/div/div[1]/a[2]");
        ac1.moveToElement(wb1).build().perform();
        wb1.click();
        Thread.sleep(3000);

        String text = driver.findElementByXPath("/html/body/div[4]/div[2]/div[1]/p").getText();
        if(text.equals("We build products that empower developers and connect them to solutions that enable productivity, growth, and discovery.")){
            System.out.println("works");
        }else{
            System.out.println("dontworks");
        }
        driver.quit();
        proxy.stop();

    } catch (InterruptedException e) {
    }
}

}
谢谢大家的帮助

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题