操作点击脚本 - Selenium

时间:2016-07-29 08:20:45

标签: java selenium firefox junit

下面的代码是我使用Selenium IDE创建的selenium脚本,我试图让它从技术按钮点击250像素,应点击“教育”选项卡。您可以通过运行脚本看到“教育”选项卡突出显示,就好像它被鼠标悬停但控制台出错:

  

org.openqa.selenium.WebDriverException:元素无法点击   点(753.5,107.51666259765625)。其他元素会收到   点击

使用 FireFox 45.0.0.1 Selenium Webdriver 2.53.1

package MyPackage;

import java.util.regex.Pattern;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class BBC {
   private String baseUrl;
   private boolean acceptNextAlert = true;
   private StringBuffer verificationErrors = new StringBuffer();
static ProfilesIni profile = new ProfilesIni();
static FirefoxProfile ffprofile = profile.getProfile("selenium");
static WebDriver driver = new FirefoxDriver(ffprofile);

   @Before
    public void setUp() throws Exception {
baseUrl = "https://www.google.co.uk/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
   public void testBBCtest2() throws Exception {
driver.get(baseUrl + "/?gfe_rd=cr&ei=h4qDV93mBOjR8gfVi4qwDg&gws_rd=ssl");
driver.findElement(By.id("lst-ib")).clear();
driver.findElement(By.id("lst-ib")).sendKeys("BBC news");
driver.findElement(By.linkText("Home - BBC News")).click();
driver.findElement(By.xpath("//div[@id='site-container']/div/div[2]/ul/li[6]/a/span")).click();
WebElement link = driver.findElement(By.xpath("//div[@id='site-container']/div/div[2]/ul/li[6]/a/span"));
Actions builder = new Actions(driver);   
builder.moveToElement(link, 250, 0).click().build().perform();
 }

 @After
  public void tearDown() throws Exception {
  String verificationErrorString = verificationErrors.toString();
  if (!"".equals(verificationErrorString)) {
  fail(verificationErrorString);
   }
 }

 private boolean isElementPresent(By by) {
   try {
  driver.findElement(by);
  return true;
   } catch (NoSuchElementException e) {
  return false;
  }
 }

 private boolean isAlertPresent() {
  try {
  driver.switchTo().alert();
  return true;
   } catch (NoAlertPresentException e) {
  return false;
   }
  } 

 private String closeAlertAndGetItsText() {
   try {
  Alert alert = driver.switchTo().alert();
  String alertText = alert.getText();
  if (acceptNextAlert) {
    alert.accept();
  } else {
    alert.dismiss();
  }
  return alertText;
  } finally {
  acceptNextAlert = true;
 }
 }
 }`

1 个答案:

答案 0 :(得分:0)

而不是:

builder.moveToElement(link, 250, 0).click().build().perform();

尝试:

builder.moveToElement(link, 250, 0);
builder.clickAndHold();
builder.release();
builder.build();
builder.perform();