我正在尝试使用 chrome 驱动程序从 graytip 门户网站以 PDF 格式下载工资单。我正在尝试使用“driver.findElement(By.linkText(“Salary”)).click();“点击链接工资。但我是无法单击链接并失败,出现以下异常。
错误
org.openqa.selenium.WebDriverException:元素在点 (198, 139) 处不可点击。其他元素将收到点击:...(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:37 毫秒
而且,当我运行脚本 chrome 时,会为 bit torrent 打开一个额外的选项卡。所以当我运行程序时,会为“ https://psdpl.greytip.in ”打开一个选项卡,而为 bittorrent 打开另一个选项卡。如何当我运行程序时,我处理不打开另一个 bittorrent 选项卡。
在这里我附上代码和屏幕截图。在此处 输入图像描述
代码
package com.webdriver.tests;
import static org.junit.Assert.*;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.regex.Pattern;
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.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class PaySlipPDF {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\soleti\\D-Drive\\Selenium\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
baseUrl = "https://psdpl.greytip.in/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testPayslip() throws Exception {
driver.get(baseUrl + "/login.do");
driver.findElement(By.id("j_username")).clear();
driver.findElement(By.id("j_username")).sendKeys("101786");
driver.findElement(By.id("j_password")).clear();
driver.findElement(By.id("j_password")).sendKeys("password");
driver.findElement(By.id("login-button")).click();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//driver.findElement(By.xpath("//*[@id='home-page']/div[1]/div[1]/ul/li[2]/a")).click();
WebElement elementToClick = driver.findElement(By.xpath("//*[@id='home-page']/div[1]/div[1]/ul/li[2]/a"));
System.out.println(elementToClick);
// Scroll the browser to the element's Y position
((JavascriptExecutor) driver).executeScript("window.scrollTo(0,"+elementToClick.getLocation().y+")");
// Click the element
elementToClick.click();
//driver.findElement(By.linkText("Salary")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.linkText("View Payslips")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
new Select(driver.findElement(By.id("payroll"))).selectByVisibleText("Mar 2012");
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.className("btn btn-gts-print")).click();
}
@After
public void tearDown() throws Exception {
//driver.quit();
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;
}
}
}