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

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

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

  1. package seleprueba;
  2. import java.io.IOException;
  3. import java.util.concurrent.TimeUnit;
  4. import net.lightbody.bmp.BrowserMobProxyServer;
  5. import net.lightbody.bmp.client.ClientUtil;
  6. import net.lightbody.bmp.proxy.auth.AuthType;
  7. import org.openqa.selenium.Proxy;
  8. import org.openqa.selenium.WebElement;
  9. import org.openqa.selenium.chrome.ChromeDriver;
  10. import org.openqa.selenium.chrome.ChromeOptions;
  11. import org.openqa.selenium.interactions.Actions;
  12. import org.openqa.selenium.remote.CapabilityType;
  13. import org.openqa.selenium.remote.DesiredCapabilities;
  14. public class SelePrueba {
  15. public static void main(String[] args) throws IOException {
  16. try {
  17. String baseUrl = "https://stackoverflow.com/questions/65080690/exception-in-thread-main-org-openqa-selenium-nosuchframeexception-no-such-fra";
  18. String driverPath = "/usr/src/scripts/ext/chromedriver";
  19. String driverName = "webdriver.chrome.driver";
  20. System.setProperty(driverName, driverPath);
  21. BrowserMobProxyServer proxy = new BrowserMobProxyServer();
  22. proxy.chainedProxyAuthorization("USERPROXY", "PASSWORDPROXY", AuthType.BASIC);
  23. proxy.start(0);
  24. Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
  25. seleniumProxy.setHttpProxy("some.proxy.example:8080");
  26. seleniumProxy.setSslProxy("some.proxy.example:8080");
  27. ChromeDriver driver;
  28. ChromeOptions chromeOptions = new ChromeOptions().setHeadless(true);
  29. DesiredCapabilities seleniumCapabilities = new DesiredCapabilities();
  30. seleniumCapabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
  31. chromeOptions.addArguments("no-sandbox");
  32. chromeOptions.addArguments("disable-dev-shm-usage");
  33. chromeOptions.addArguments("--proxy-server=http://some.proxy.example:8080");
  34. chromeOptions.merge(seleniumCapabilities);
  35. driver = new ChromeDriver(chromeOptions);
  36. driver.get(baseUrl);
  37. driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  38. Actions ac1 = new Actions(driver);
  39. WebElement wb1 = driver.findElementByXPath("/html/body/header/div/div[1]/a[2]");
  40. ac1.moveToElement(wb1).build().perform();
  41. wb1.click();
  42. Thread.sleep(3000);
  43. String text = driver.findElementByXPath("/html/body/div[4]/div[2]/div[1]/p").getText();
  44. if(text.equals("We build products that empower developers and connect them to solutions that enable productivity, growth, and discovery.")){
  45. System.out.println("works");
  46. }else{
  47. System.out.println("dontworks");
  48. }
  49. driver.quit();
  50. proxy.stop();
  51. } catch (InterruptedException e) {
  52. }
  53. }

}
谢谢大家的帮助

暂无答案!

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

相关问题