问题
The code below is a selenium script I created with the Selenium IDE, I'm trying to get it to click 250 pixels from the technology button which should click the 'education' tab. You can see by running the script that the 'education' tab is highlighted as if its moused over but the console gives the error:
org.openqa.selenium.WebDriverException: Element is not clickable at point (753.5, 107.51666259765625). Other element would receive the click
Using:
- 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:
Instead of :
builder.moveToElement(link, 250, 0).click().build().perform();
Try :
builder.moveToElement(link, 250, 0);
builder.clickAndHold();
builder.release();
builder.build();
builder.perform();
来源:https://stackoverflow.com/questions/38653910/actions-click-script-selenium