<p><ahref=/documentation/about/contributing/#moving-examples><spanclass="selenium-badge-code"data-bs-toggle="tooltip"data-bs-placement="right"title="One or more of these examples need to be implemented in the examples directory; click for details in the contribution guide">MoveCode</span></a></p>valoptions=InternetExplorerOptions()valdriver=InternetExplorerDriver(options)
以下是几种具有不同功能的常见用例:
fileUploadDialogTimeout
在某些环境中, 当打开文件上传对话框时, Internet Explorer可能会超时. IEDriver的默认超时为1000毫秒, 但您可以使用fileUploadDialogTimeout功能来增加超时时间.
importosimportsubprocessimportsysimportpytestfromseleniumimportwebdriverEDGE_BINARY=os.getenv("EDGE_BINARY")@pytest.mark.skipif(sys.platform!="win32",reason="requires Windows")deftest_basic_options_win10():options=webdriver.IeOptions()options.attach_to_edge_chrome=Trueoptions.edge_executable_path=EDGE_BINARYdriver=webdriver.Ie(options=options)driver.quit()@pytest.mark.skipif(sys.platform!="win32",reason="requires Windows")deftest_basic_options_win11():options=webdriver.IeOptions()driver=webdriver.Ie(options=options)driver.quit()@pytest.mark.skipif(sys.platform!="win32",reason="requires Windows")deftest_log_to_file(log_path):service=webdriver.ie.service.Service(log_file=log_path,log_level='INFO')driver=webdriver.Ie(service=service)withopen(log_path,'r')asfp:assert"Starting WebDriver server"infp.readline()driver.quit()@pytest.mark.skip(reason="this is not supported, yet")deftest_log_to_stdout(capfd):service=webdriver.ie.service.Service(log_output=subprocess.STDOUT)driver=webdriver.Ie(service=service)out,err=capfd.readouterr()assert"Started InternetExplorerDriver server"inoutdriver.quit()@pytest.mark.skipif(sys.platform!="win32",reason="requires Windows")deftest_log_level(log_path):service=webdriver.ie.service.Service(log_file=log_path,log_level='WARN')driver=webdriver.Ie(service=service)withopen(log_path,'r')asfp:assert'Invalid capability setting: timeouts is type null'infp.readline()driver.quit()@pytest.mark.skipif(sys.platform!="win32",reason="requires Windows")deftest_supporting_files(temp_dir):service=webdriver.ie.service.Service(service_args=["–extract-path="+temp_dir])driver=webdriver.Ie(service=service)driver.quit()
packagedev.selenium.browsers;importorg.junit.jupiter.api.AfterEach;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.Test;importorg.junit.jupiter.api.condition.EnabledOnOs;importorg.junit.jupiter.api.condition.OS;importorg.openqa.selenium.ie.InternetExplorerDriver;importorg.openqa.selenium.ie.InternetExplorerDriverLogLevel;importorg.openqa.selenium.ie.InternetExplorerDriverService;importorg.openqa.selenium.ie.InternetExplorerOptions;importjava.io.File;importjava.io.IOException;importjava.io.PrintStream;importjava.nio.file.Files;@EnabledOnOs(OS.WINDOWS)publicclassInternetExplorerTest{publicInternetExplorerDriverdriver;privateFilelogLocation;privateFiletempDirectory;@AfterEachpublicvoidquit(){if(logLocation!=null&&logLocation.exists()){logLocation.delete();}if(tempDirectory!=null&&tempDirectory.exists()){tempDirectory.delete();}driver.quit();}@TestpublicvoidbasicOptionsWin10(){InternetExplorerOptionsoptions=newInternetExplorerOptions();options.attachToEdgeChrome();options.withEdgeExecutablePath(System.getProperty("webdriver.edge.binary"));driver=newInternetExplorerDriver(options);}@TestpublicvoidbasicOptionsWin11(){InternetExplorerOptionsoptions=newInternetExplorerOptions();driver=newInternetExplorerDriver(options);}@TestpublicvoidlogsToFile()throwsIOException{InternetExplorerDriverServiceservice=newInternetExplorerDriverService.Builder().withLogFile(getLogLocation()).build();driver=newInternetExplorerDriver(service);StringfileContent=newString(Files.readAllBytes(getLogLocation().toPath()));Assertions.assertTrue(fileContent.contains("Started InternetExplorerDriver server"));}@TestpublicvoidlogsToConsole()throwsIOException{System.setOut(newPrintStream(getLogLocation()));InternetExplorerDriverServiceservice=newInternetExplorerDriverService.Builder().withLogOutput(System.out).build();driver=newInternetExplorerDriver(service);StringfileContent=newString(Files.readAllBytes(getLogLocation().toPath()));Assertions.assertTrue(fileContent.contains("Started InternetExplorerDriver server"));}@TestpublicvoidlogsWithLevel()throwsIOException{System.setProperty(InternetExplorerDriverService.IE_DRIVER_LOGFILE_PROPERTY,getLogLocation().getAbsolutePath());InternetExplorerDriverServiceservice=newInternetExplorerDriverService.Builder().withLogLevel(InternetExplorerDriverLogLevel.WARN).build();driver=newInternetExplorerDriver(service);StringfileContent=newString(Files.readAllBytes(getLogLocation().toPath()));Assertions.assertTrue(fileContent.contains("Invalid capability setting: timeouts is type null"));}@TestpublicvoidsupportingFilesLocation()throwsIOException{InternetExplorerDriverServiceservice=newInternetExplorerDriverService.Builder().withExtractPath(getTempDirectory()).build();driver=newInternetExplorerDriver(service);Assertions.assertTrue(newFile(getTempDirectory()+"/IEDriver.tmp").exists());}privateFilegetLogLocation()throwsIOException{if(logLocation==null||!logLocation.exists()){logLocation=File.createTempFile("iedriver-",".log");}returnlogLocation;}privateFilegetTempDirectory()throwsIOException{if(tempDirectory==null||!tempDirectory.exists()){tempDirectory=Files.createTempDirectory("supporting-").toFile();}returntempDirectory;}}
Note: Java also allows setting console output by System Property; Property key: InternetExplorerDriverService.IE_DRIVER_LOGFILE_PROPERTY Property value: DriverService.LOG_STDOUT or DriverService.LOG_STDERR
importosimportsubprocessimportsysimportpytestfromseleniumimportwebdriverEDGE_BINARY=os.getenv("EDGE_BINARY")@pytest.mark.skipif(sys.platform!="win32",reason="requires Windows")deftest_basic_options_win10():options=webdriver.IeOptions()options.attach_to_edge_chrome=Trueoptions.edge_executable_path=EDGE_BINARYdriver=webdriver.Ie(options=options)driver.quit()@pytest.mark.skipif(sys.platform!="win32",reason="requires Windows")deftest_basic_options_win11():options=webdriver.IeOptions()driver=webdriver.Ie(options=options)driver.quit()@pytest.mark.skipif(sys.platform!="win32",reason="requires Windows")deftest_log_to_file(log_path):service=webdriver.ie.service.Service(log_file=log_path,log_level='INFO')driver=webdriver.Ie(service=service)withopen(log_path,'r')asfp:assert"Starting WebDriver server"infp.readline()driver.quit()@pytest.mark.skip(reason="this is not supported, yet")deftest_log_to_stdout(capfd):service=webdriver.ie.service.Service(log_output=subprocess.STDOUT)driver=webdriver.Ie(service=service)out,err=capfd.readouterr()assert"Started InternetExplorerDriver server"inoutdriver.quit()@pytest.mark.skipif(sys.platform!="win32",reason="requires Windows")deftest_log_level(log_path):service=webdriver.ie.service.Service(log_file=log_path,log_level='WARN')driver=webdriver.Ie(service=service)withopen(log_path,'r')asfp:assert'Invalid capability setting: timeouts is type null'infp.readline()driver.quit()@pytest.mark.skipif(sys.platform!="win32",reason="requires Windows")deftest_supporting_files(temp_dir):service=webdriver.ie.service.Service(service_args=["–extract-path="+temp_dir])driver=webdriver.Ie(service=service)driver.quit()
packagedev.selenium.browsers;importorg.junit.jupiter.api.AfterEach;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.Test;importorg.junit.jupiter.api.condition.EnabledOnOs;importorg.junit.jupiter.api.condition.OS;importorg.openqa.selenium.ie.InternetExplorerDriver;importorg.openqa.selenium.ie.InternetExplorerDriverLogLevel;importorg.openqa.selenium.ie.InternetExplorerDriverService;importorg.openqa.selenium.ie.InternetExplorerOptions;importjava.io.File;importjava.io.IOException;importjava.io.PrintStream;importjava.nio.file.Files;@EnabledOnOs(OS.WINDOWS)publicclassInternetExplorerTest{publicInternetExplorerDriverdriver;privateFilelogLocation;privateFiletempDirectory;@AfterEachpublicvoidquit(){if(logLocation!=null&&logLocation.exists()){logLocation.delete();}if(tempDirectory!=null&&tempDirectory.exists()){tempDirectory.delete();}driver.quit();}@TestpublicvoidbasicOptionsWin10(){InternetExplorerOptionsoptions=newInternetExplorerOptions();options.attachToEdgeChrome();options.withEdgeExecutablePath(System.getProperty("webdriver.edge.binary"));driver=newInternetExplorerDriver(options);}@TestpublicvoidbasicOptionsWin11(){InternetExplorerOptionsoptions=newInternetExplorerOptions();driver=newInternetExplorerDriver(options);}@TestpublicvoidlogsToFile()throwsIOException{InternetExplorerDriverServiceservice=newInternetExplorerDriverService.Builder().withLogFile(getLogLocation()).build();driver=newInternetExplorerDriver(service);StringfileContent=newString(Files.readAllBytes(getLogLocation().toPath()));Assertions.assertTrue(fileContent.contains("Started InternetExplorerDriver server"));}@TestpublicvoidlogsToConsole()throwsIOException{System.setOut(newPrintStream(getLogLocation()));InternetExplorerDriverServiceservice=newInternetExplorerDriverService.Builder().withLogOutput(System.out).build();driver=newInternetExplorerDriver(service);StringfileContent=newString(Files.readAllBytes(getLogLocation().toPath()));Assertions.assertTrue(fileContent.contains("Started InternetExplorerDriver server"));}@TestpublicvoidlogsWithLevel()throwsIOException{System.setProperty(InternetExplorerDriverService.IE_DRIVER_LOGFILE_PROPERTY,getLogLocation().getAbsolutePath());InternetExplorerDriverServiceservice=newInternetExplorerDriverService.Builder().withLogLevel(InternetExplorerDriverLogLevel.WARN).build();driver=newInternetExplorerDriver(service);StringfileContent=newString(Files.readAllBytes(getLogLocation().toPath()));Assertions.assertTrue(fileContent.contains("Invalid capability setting: timeouts is type null"));}@TestpublicvoidsupportingFilesLocation()throwsIOException{InternetExplorerDriverServiceservice=newInternetExplorerDriverService.Builder().withExtractPath(getTempDirectory()).build();driver=newInternetExplorerDriver(service);Assertions.assertTrue(newFile(getTempDirectory()+"/IEDriver.tmp").exists());}privateFilegetLogLocation()throwsIOException{if(logLocation==null||!logLocation.exists()){logLocation=File.createTempFile("iedriver-",".log");}returnlogLocation;}privateFilegetTempDirectory()throwsIOException{if(tempDirectory==null||!tempDirectory.exists()){tempDirectory=Files.createTempDirectory("supporting-").toFile();}returntempDirectory;}}
Note: Java also allows setting log level by System Property: Property key: InternetExplorerDriverService.IE_DRIVER_LOGLEVEL_PROPERTY Property value: String representation of InternetExplorerDriverLogLevel.DEBUG.toString() enum
importosimportsubprocessimportsysimportpytestfromseleniumimportwebdriverEDGE_BINARY=os.getenv("EDGE_BINARY")@pytest.mark.skipif(sys.platform!="win32",reason="requires Windows")deftest_basic_options_win10():options=webdriver.IeOptions()options.attach_to_edge_chrome=Trueoptions.edge_executable_path=EDGE_BINARYdriver=webdriver.Ie(options=options)driver.quit()@pytest.mark.skipif(sys.platform!="win32",reason="requires Windows")deftest_basic_options_win11():options=webdriver.IeOptions()driver=webdriver.Ie(options=options)driver.quit()@pytest.mark.skipif(sys.platform!="win32",reason="requires Windows")deftest_log_to_file(log_path):service=webdriver.ie.service.Service(log_file=log_path,log_level='INFO')driver=webdriver.Ie(service=service)withopen(log_path,'r')asfp:assert"Starting WebDriver server"infp.readline()driver.quit()@pytest.mark.skip(reason="this is not supported, yet")deftest_log_to_stdout(capfd):service=webdriver.ie.service.Service(log_output=subprocess.STDOUT)driver=webdriver.Ie(service=service)out,err=capfd.readouterr()assert"Started InternetExplorerDriver server"inoutdriver.quit()@pytest.mark.skipif(sys.platform!="win32",reason="requires Windows")deftest_log_level(log_path):service=webdriver.ie.service.Service(log_file=log_path,log_level='WARN')driver=webdriver.Ie(service=service)withopen(log_path,'r')asfp:assert'Invalid capability setting: timeouts is type null'infp.readline()driver.quit()@pytest.mark.skipif(sys.platform!="win32",reason="requires Windows")deftest_supporting_files(temp_dir):service=webdriver.ie.service.Service(service_args=["–extract-path="+temp_dir])driver=webdriver.Ie(service=service)driver.quit()
usingSystem;usingSystem.IO;usingSystem.Linq;usingMicrosoft.VisualStudio.TestTools.UnitTesting;usingOpenQA.Selenium.IE;usingSeleniumDocs.TestSupport;namespaceSeleniumDocs.Browsers{ [TestClassCustom] [EnabledOnOs("WINDOWS")]publicclassInternetExplorerTest{privateInternetExplorerDriverdriver;privatestring_logLocation;privatestring_tempPath; [TestCleanup]publicvoidCleanup(){if(_logLocation!=null&&File.Exists(_logLocation)){File.Delete(_logLocation);}if(_tempPath!=null&&File.Exists(_tempPath)){File.Delete(_tempPath);}driver.Quit();} [TestMethod]publicvoidBasicOptionsWin10(){varoptions=newInternetExplorerOptions();options.AttachToEdgeChrome=true;options.EdgeExecutablePath=Environment.GetEnvironmentVariable("EDGE_BINARY");driver=newInternetExplorerDriver(options);} [TestMethod]publicvoidBasicOptionsWin11(){varoptions=newInternetExplorerOptions();driver=newInternetExplorerDriver(options);} [TestMethod] [Ignore("Not implemented")]publicvoidLogsToFile(){varservice=InternetExplorerDriverService.CreateDefaultService();service.LogFile=GetLogLocation();driver=newInternetExplorerDriver(service);driver.Quit();// Close the Service log file before readingvarlines=File.ReadLines(GetLogLocation());Assert.IsNotNull(lines.FirstOrDefault(line=>line.Contains("geckodriver INFO Listening on")));} [TestMethod] [Ignore("Not implemented")]publicvoidLogsToConsole(){varstringWriter=newStringWriter();varoriginalOutput=Console.Out;Console.SetOut(stringWriter);varservice=InternetExplorerDriverService.CreateDefaultService();//service.LogToConsole = true;driver=newInternetExplorerDriver(service);Assert.IsTrue(stringWriter.ToString().Contains("geckodriver INFO Listening on"));Console.SetOut(originalOutput);stringWriter.Dispose();} [TestMethod]publicvoidLogsLevel(){varservice=InternetExplorerDriverService.CreateDefaultService();service.LogFile=GetLogLocation();service.LoggingLevel=InternetExplorerDriverLogLevel.Warn;driver=newInternetExplorerDriver(service);driver.Quit();// Close the Service log file before readingvarlines=File.ReadLines(GetLogLocation());Assert.IsNotNull(lines.FirstOrDefault(line=>line.Contains("Invalid capability setting: timeouts is type null")));} [TestMethod]publicvoidSupportingFilesLocation(){varservice=InternetExplorerDriverService.CreateDefaultService();service.LibraryExtractionPath=GetTempDirectory();driver=newInternetExplorerDriver(service);Assert.IsTrue(File.Exists(GetTempDirectory()+"/IEDriver.tmp"));}privatestringGetLogLocation(){if(_logLocation==null||!File.Exists(_logLocation)){_logLocation=Path.GetTempFileName();}return_logLocation;}privatestringGetTempDirectory(){if(_tempPath==null||!File.Exists(_tempPath)){_tempPath=Path.GetTempPath();}return_tempPath;}}}
importosimportsubprocessimportsysimportpytestfromseleniumimportwebdriverEDGE_BINARY=os.getenv("EDGE_BINARY")@pytest.mark.skipif(sys.platform!="win32",reason="requires Windows")deftest_basic_options_win10():options=webdriver.IeOptions()options.attach_to_edge_chrome=Trueoptions.edge_executable_path=EDGE_BINARYdriver=webdriver.Ie(options=options)driver.quit()@pytest.mark.skipif(sys.platform!="win32",reason="requires Windows")deftest_basic_options_win11():options=webdriver.IeOptions()driver=webdriver.Ie(options=options)driver.quit()@pytest.mark.skipif(sys.platform!="win32",reason="requires Windows")deftest_log_to_file(log_path):service=webdriver.ie.service.Service(log_file=log_path,log_level='INFO')driver=webdriver.Ie(service=service)withopen(log_path,'r')asfp:assert"Starting WebDriver server"infp.readline()driver.quit()@pytest.mark.skip(reason="this is not supported, yet")deftest_log_to_stdout(capfd):service=webdriver.ie.service.Service(log_output=subprocess.STDOUT)driver=webdriver.Ie(service=service)out,err=capfd.readouterr()assert"Started InternetExplorerDriver server"inoutdriver.quit()@pytest.mark.skipif(sys.platform!="win32",reason="requires Windows")deftest_log_level(log_path):service=webdriver.ie.service.Service(log_file=log_path,log_level='WARN')driver=webdriver.Ie(service=service)withopen(log_path,'r')asfp:assert'Invalid capability setting: timeouts is type null'infp.readline()driver.quit()@pytest.mark.skipif(sys.platform!="win32",reason="requires Windows")deftest_supporting_files(temp_dir):service=webdriver.ie.service.Service(service_args=["–extract-path="+temp_dir])driver=webdriver.Ie(service=service)driver.quit()
usingSystem;usingSystem.IO;usingSystem.Linq;usingMicrosoft.VisualStudio.TestTools.UnitTesting;usingOpenQA.Selenium.IE;usingSeleniumDocs.TestSupport;namespaceSeleniumDocs.Browsers{ [TestClassCustom] [EnabledOnOs("WINDOWS")]publicclassInternetExplorerTest{privateInternetExplorerDriverdriver;privatestring_logLocation;privatestring_tempPath; [TestCleanup]publicvoidCleanup(){if(_logLocation!=null&&File.Exists(_logLocation)){File.Delete(_logLocation);}if(_tempPath!=null&&File.Exists(_tempPath)){File.Delete(_tempPath);}driver.Quit();} [TestMethod]publicvoidBasicOptionsWin10(){varoptions=newInternetExplorerOptions();options.AttachToEdgeChrome=true;options.EdgeExecutablePath=Environment.GetEnvironmentVariable("EDGE_BINARY");driver=newInternetExplorerDriver(options);} [TestMethod]publicvoidBasicOptionsWin11(){varoptions=newInternetExplorerOptions();driver=newInternetExplorerDriver(options);} [TestMethod] [Ignore("Not implemented")]publicvoidLogsToFile(){varservice=InternetExplorerDriverService.CreateDefaultService();service.LogFile=GetLogLocation();driver=newInternetExplorerDriver(service);driver.Quit();// Close the Service log file before readingvarlines=File.ReadLines(GetLogLocation());Assert.IsNotNull(lines.FirstOrDefault(line=>line.Contains("geckodriver INFO Listening on")));} [TestMethod] [Ignore("Not implemented")]publicvoidLogsToConsole(){varstringWriter=newStringWriter();varoriginalOutput=Console.Out;Console.SetOut(stringWriter);varservice=InternetExplorerDriverService.CreateDefaultService();//service.LogToConsole = true;driver=newInternetExplorerDriver(service);Assert.IsTrue(stringWriter.ToString().Contains("geckodriver INFO Listening on"));Console.SetOut(originalOutput);stringWriter.Dispose();} [TestMethod]publicvoidLogsLevel(){varservice=InternetExplorerDriverService.CreateDefaultService();service.LogFile=GetLogLocation();service.LoggingLevel=InternetExplorerDriverLogLevel.Warn;driver=newInternetExplorerDriver(service);driver.Quit();// Close the Service log file before readingvarlines=File.ReadLines(GetLogLocation());Assert.IsNotNull(lines.FirstOrDefault(line=>line.Contains("Invalid capability setting: timeouts is type null")));} [TestMethod]publicvoidSupportingFilesLocation(){varservice=InternetExplorerDriverService.CreateDefaultService();service.LibraryExtractionPath=GetTempDirectory();driver=newInternetExplorerDriver(service);Assert.IsTrue(File.Exists(GetTempDirectory()+"/IEDriver.tmp"));}privatestringGetLogLocation(){if(_logLocation==null||!File.Exists(_logLocation)){_logLocation=Path.GetTempFileName();}return_logLocation;}privatestringGetTempDirectory(){if(_tempPath==null||!File.Exists(_tempPath)){_tempPath=Path.GetTempPath();}return_tempPath;}}}