Tag: selenium webdriver

使用selenium Java(Mac OSX)将Firefox浏览器置于前端

我正在使用三个火狐驱动程序实例进行自动化。我需要将当前活动的firefox浏览器放在前面,因为我正在使用一些机器人类进行一些操作。 我在mac(同样的操作)中尝试了谷歌浏览器的java脚本警报,并且工作正常。 在windows中使用user32 lib。 在firefox mac的情况下,它在后台显示警报,但网页没有出现在前面。 ((JavascriptExecutor)this.webDriver).executeScript(“alert(‘Test’)”); this.webDriver.switchTo().alert().accept(); 上面的代码我用于Mac中的chrome。 相同的代码正在工作并显示firefox的警报,但窗口没有出现在前面。 请建议是否有任何其他方法在Firefox中执行相同的操作。

如何解决,陈旧元素exception? 如果元素不再附加到DOM?

我有一个关于“元素不再附加到DOM”的问题。 我尝试了不同的解决方案,但他们间歇性地工作。 请建议一个永久性的解决方案。 WebElement getStaleElemById(String id, WebDriver driver) { try { return driver.findElement(By.id(id)); } catch (StaleElementReferenceException e) { System.out.println(“Attempting to recover from StaleElementReferenceException …”); return getStaleElemById(id, driver); } } WebElement getStaleElemByCss(String css, WebDriver driver) { try { return driver.findElement(By.cssSelector(css)); } catch (StaleElementReferenceException e) { System.out.println(“Attempting to recover from StaleElementReferenceException …”); return getStaleElemByCss(css, driver); } catch […]

如何使用selenium webdriver在chrome中下载pdf文件

我想使用selenium在chrome中下载pdf。 System.setProperty(“webdriver.chrome.driver”, System.getProperty(“user.dir”) + System.getProperty(“file.separator”) + “BrowserDrivers” + System.getProperty(“file.separator”) + “chromedriver.exe”); String downloadFilepath = “C:\\Users\\Vinod\\Downloads”; HashMap chromePrefs = new HashMap(); chromePrefs.put(“profile.default_content_settings.popups”, 0); chromePrefs.put(“download.default_directory”, downloadFilepath); //Save Chrome Opions ChromeOptions options = new ChromeOptions(); HashMap chromeOptionsMap = new HashMap(); options.setExperimentalOption(“prefs”, chromePrefs); options.addArguments(“–test-type”); DesiredCapabilities cap = DesiredCapabilities.chrome(); cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap); cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); cap.setCapability(ChromeOptions.CAPABILITY, options); driver = new ChromeDriver(cap); driver.get(url); […]

如何使用selenium将javascript文件加载到DOM中?

我正在使用Selenium WebDriver尝试将外部javascript文件插入到DOM中,而不是将整个内容输入到executeScript中。 看起来它正确地将节点放入DOM中,但它只是忽略了源,即所述源js文件上的函数不运行。 这是我的代码: import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Example { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get(“http://google.com”); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript(“document.getElementsByTagName(‘head’)[0].innerHTML += ‘<script src=\"\” type=\”text/javascript\”>’;”); } } 我链接的javascript文件的代码是 alert(“hi Nate”); 我已将js文件放在我的localhost上,我使用file:///调用它,然后在外部服务器上尝试了它。 没有骰子。 此外,在Java部分,我尝试使用该技巧附加’scr’+’ipt’,但它仍然无效。 当我使用Firefox的inspect元素检查DOM时,我可以看到它正确加载脚本节点,所以我很困惑。 我也尝试过这个解决方案,它显然是为另一个版本的Selenium(不是webdriver)制作的,因此无法在最低限度下工作: 在selenium中加载包含有用测试函数的外部js文件

如何在Selenium WebDriver中获取本机记录器

是否有可能以某种方式获取Selenium WebDriver使用的记录器? 我想捕获所有发出的命令的记录(例如:打开,等待,点击等)。 特别是我正在寻找一个java解决方案,因为我将测试导出到junit。 我在他们的网站上找到了这个代码,但它没有标准显示 DesiredCapabilities caps = DesiredCapabilities.firefox(); LoggingPreferences logs = new LoggingPreferences(); logs.enable(LogType.DRIVER, Level.FINEST); caps.setCapability(CapabilityType.LOGGING_PREFS, logs); driver = new FirefoxDriver(caps);

如何validationselenium2中存在或可见的元素(Selenium WebDriver)

任何人都可以给我发送示例代码如何validation元素 存在 可见性 isenable textpresent 在Selenium WebDrvier中使用Java

如何在使用chrome driver / firefox驱动程序时更改Webdriver中的文件下载位置

我试图通过在特定文件夹中使用另存为选项来保存图像。 我找到了一种方法,通过该选项,我可以右键单击要保存的图像。 但我遇到的问题是在获取os窗口后询问保存文件的位置,我无法发送所需的位置,因为我不知道该怎么做。 我经历了在这个论坛上提出的类似问题,但到目前为止他们没有帮助过。 代码是 – 对于Firefox- public class practice { public void pic() throws AWTException{ WebDriver driver; //Proxy Setting FirefoxProfile profile = new FirefoxProfile(); profile.setAssumeUntrustedCertificateIssuer(false); profile.setEnableNativeEvents(false); profile.setPreference(“network.proxy.type”, 1); profile.setPreference(“network.proxy.http”, “localHost”); profile.setPreference(“newtwork.proxy.http_port”,3128); //Download setting profile.setPreference(“browser.download.folderlist”, 2); profile.setPreference(“browser.helperapps.neverAsk.saveToDisk”,”jpeg”); profile.setPreference(“browser.download.dir”, “C:\\Users\\Admin\\Desktop\\ScreenShot\\pic.jpeg”); driver = new FirefoxDriver(profile); driver.navigate().to(“http://stackoverflow.com/users/2675355/shantanu”); driver.findElement(By.xpath(“//*[@id=’large-user-info’]/div[1]/div[1]/a/div/img”)); Actions action = new Actions(driver); action.moveToElement(driver.findElement(By.xpath(“//*[@id=’large-user-info’]/div[1]/div[1]/a/div/img”))).perform(); action.contextClick().perform(); Robot robo = […]

在Selenium2中是否有针对FirefoxDriver的经过validation的mouseOver解决方法?

我正在使用Selenium Java 2.0b3 。 我有这个代码: … WebDriver driver = new InternetExplorerDriver(); Selenium seleniumDriver = new WebDriverBackedSelenium(driver, “http://localhost:8088/Sistema/”); … … RenderedWebElement menuRegistrar = (RenderedWebElement)driver.findElement(By.xpath(“//a[normalize-space()=’Registrar’]”)); seleniumDriver.mouseOver(“//a[normalize-space()=’Registrar’]”); //makes element visible menuRegistrar.click(); seleniumDriver.mouseOut(“//a[normalize-space()=’Registrar’]”); … 与InternetExplorerDriver(使用IE 8 )的魅力相似,但它不适用于FirefoxDriver(使用Firefox 4 )。 我用代码尝试了很多东西,没有任何作用。 我必须使用FirefoxDriver,因为我正在测试的应用程序在IE上表现不佳。 正如您可能猜到的那样,“注册器”链接将被隐藏,直到mouseOver事件触发。 任何经证实的解决方法? 谢谢你的时间… 编辑 :还试用ChromeDriver与Chrome 11.也没用。 如果有适用于Chrome的解决方法,我会接受它! 答案(Selenium Java 2.0RC1,Windows 7,Firefox 4的工作代码) :感谢Andy Tinkham和Luke Inman-Semerau: //get the element […]

Selenium等待加载Ajax内容 – 通用方法

是否有一种通用的方法让Selenium等到所有ajax内容都加载完毕? (不依赖于特定的网站 – 所以它适用于每个ajax网站)

WebDriver中不支持复合类名称错误

我有一种方法来计算divs的元素数量并返回它们的数量。 public int getNumberOfOpenBets() { openBetsSlip = driver.findElement(By.id(“form_open_bets”)); openBets = openBetsSlip.findElements(By.className(” cashout_noCash”)); return openBets.size(); } 这是页面源 WebDriver抛出以下错误:不支持复合类名称。 考虑搜索一个类名并过滤结果或使用CSS选择器。 org.openqa.selenium.InvalidSelectorException: Compound class names are not supported. Consider searching for one class name and filtering the results or use CSS selectors. For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html Build info: version: ‘2.31.0’, revision: ‘1bd294d185a80fa4206dfeab80ba773c04ac33c0’, time: ‘2013-02-27 […]