WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://stackoverflow.com/");
driver.findElement(By.linkText("log in")).click();
driver.findElement(By.cssSelector("a.google.openid_large_btn")).click();
driver.findElement(By.id("Email")).sendKeys("username@gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("password");
driver.findElement(By.id("hlinks-user")).click();
driver.findElement(By.linkText("user2131252")).click();
Thread.sleep(3000L);
// None of these work to click on the upload button , actions, robot..
//driver.findElement(By.xpath(".//*[@id='change-picture']")).click();
//Actions actions = new Actions(driver);
//WebElement changepic = driver.findElement(By.xpath(".//*[@id='change-picture']"));
//actions.moveToElement(changepic).click().perform();
//actions.click();
//actions.perform();
/*Point coordinates = driver.findElement(By.xpath(".//*[@id='change-picture']")).getLocation();
Robot robot = new Robot();
robot.mouseMove(coordinates.getX(),coordinates.getY()+120);
//robot.mousePress(coordinates.getX());
driver.findElement(By.xpath(".//*[@id='change-picture']")).clear();
*/
Actions actions = new Actions(driver);
//for hovering over the username field
WebElement menuHoverLink = driver.findElement(By.xpath(".//*[@id='change- picture']"));
actions.moveToElement(menuHoverLink).perform();
//for clicking the logout link
WebElement logoutLink = driver.findElement(By.xpath(".//*[@id='change-picture']"));
logoutLink.click();
我无法点击更改图片链接,我在不同的浏览器上遇到不同的错误。
log4j:WARN找不到logger(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)的appender。 log4j:WARN请正确初始化log4j系统。 线程“main”中的异常org.openqa.selenium.interactions.MoveTargetOutOfBoundsException:元素无法滚动到视图中:(警告:服务器未提供任何堆栈跟踪信息) 命令持续时间或超时:13毫秒 构建信息:版本:'2.33.0',修订版:'4e90c97',时间:'2013-05-22 15:33:32' 系统信息:os.name:'Windows 7',os.arch:'amd64',os.version:'6.1',java.version:'1.7.0_17' 会议ID:4534c06c-0622-4610-8d7d-1cf99f9577ee 驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver Capabilities [{platform = XP,databaseEnabled = true,cssSelectorsEnabled = true,javascriptEnabled = true,acceptSslCerts = true,handlesAlerts = true,browserName = firefox,browserConnectionEnabled = true,nativeEvents = true,webStorageEnabled = true,rotate = false,locationContextEnabled = true ,applicationCacheEnabled = true,takeScreenshot = true,version = 24.0}] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) 在org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191) 在org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145) 在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554) 在org.openqa.selenium.remote.RemoteExecuteMethod.execute(RemoteExecuteMethod.java:34) 在org.openqa.selenium.remote.RemoteMouse.mouseMove(RemoteMouse.java:88) at org.openqa.selenium.interactions.MoveMouseAction.perform(MoveMouseAction.java:36) at org.openqa.selenium.interactions.CompositeAction.perform(CompositeAction.java:31) 在org.openqa.selenium.interactions.Actions.perform(Actions.java:356) 在test.stackoverflow.main(stackoverflow.java:52)
答案 0 :(得分:0)
你可以在sleep()之后尝试使用javascriptExcecuter
,
((JavascriptExecutor)driver).executeScript("$('#change-picture').click();");