问题描述
在我之前的 [问题][1] 之后,我只是认为我的测试不起作用,因为由于某种原因 Selenium 和 Java 没有连接自己,几天前我意识到问题仅在我使用我的 browsermob_proxy 时在 CentOS7 Docker 中(当我在 Windows 中使用它进行测试时,它运行良好)。
我有这个小例子,我希望有人能帮助我,我设置了“http_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) {
}
}
}
谢谢大家的帮助
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)