我正在尝试使用Selenium Webdriver上传图像,但是在上传个人资料图片时遇到“元素不可交互”的问题: enter image description here
我的硒代码在这里: 档案:1
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
public class LoginPage {
public WebDriver driver;
By email = By.name("email");
By pass = By.name("password");
By sub = By.xpath("//button[text()='Sign In']");
By view_cat = By.xpath("//div[@class='main-viewCategory-Btn']/button");
By search = By.xpath("//div[@class='search_banner_block']/input");
By search_cat = By.xpath("//div[@class='chooseCatName']/h4[text()='Finance']");
By change_pwd = By.xpath("//div[@id='navbarResponsive']/ul/li[3]");
By Old_pwd = By.xpath("//form[@name='resetPasswordForm']/div/input[@name='oldPassword']");
By new_pwd = By.xpath("//form[@name='resetPasswordForm']/div[2]/input[@name='password']");
By cnfrm = By.xpath("//div[@class='confirm-password']/input[@name='confirmPassword']");
By submit = By.xpath("//button[@type='submit']");
By toast_msg = By.xpath("//div[@id='toast-container']");
By my_profile = By.xpath("//div[@id='navbarResponsive']/ul/li/a[text()='My Profile']");
By dob = By.xpath("//input[@name='tutorDob']");
By current_date = By.xpath("//button[@class='current']");
By previous = By.xpath("//button[@class='previous']");
By year = By.xpath("//table[@class='years']/tbody/tr/td/span[text()='1996']");
By month = By.xpath("//table[@class='months']/tbody/tr/td/span[text()='January']");
By date = By.xpath("//div[@class='bs-datepicker-body']/table/tbody/tr/td[@class='ng-star-inserted'][3]/span[text()='2']");
By city = By.xpath("//input[@name='city']");
By gender = By.xpath("//select[@name='gender']");
By skype = By.xpath("//input[@name='skype']");
By img = By.xpath("//label[@class='cursor']");
By subj = By.xpath("//form[@id='msform']/div/form/div[6]/div/button[@type='button']");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void enterEmail(String em) {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(email).sendKeys(em);
}
public void enterPassword(String pw) {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(pass).sendKeys(pw);
}
public void clickLogin() {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(sub).click();
}
public void all_cat() throws InterruptedException {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(view_cat).click();
}
public void search_cat(String cat_search) throws InterruptedException {
Thread.sleep(5000);
//driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(search).sendKeys(cat_search);
}
public void search_cat1() {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(search_cat).click();
}
public void change_password() {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(change_pwd).click();
}
public void old_password(String old) throws InterruptedException {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(Old_pwd).sendKeys(old);
}
public void new_password(String newpwd) throws InterruptedException {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(new_pwd).sendKeys(newpwd);
}
public void confirm_password(String confirmpwd) throws InterruptedException {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(cnfrm).sendKeys(confirmpwd);
}
public void submit() throws InterruptedException {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(submit).click();
}
public void toaster() throws InterruptedException {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
System.out.println(driver.findElement(toast_msg).getText());
}
public void profile() throws InterruptedException {
System.out.println("Profile click");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(my_profile).click();
}
public void select_dob() throws InterruptedException {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement element = driver.findElement(dob);
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
// driver.findElement(dob).click();
}
public void select_currentdate() throws InterruptedException {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement element = driver.findElement(current_date);
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
// driver.findElement(current_date).click();
}
public void select_previous() throws InterruptedException {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement element = driver.findElement(previous);
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
// driver.findElement(previous).click();
}
public void select_year() throws InterruptedException {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement element = driver.findElement(year);
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
// driver.findElement(year).click();
}
public void select_month() throws InterruptedException {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement element = driver.findElement(month);
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
// driver.findElement(month).click();
}
public void select_date() throws InterruptedException {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement element = driver.findElement(date);
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
// driver.findElement(date).click();
}
public void select_city(String new_city) throws InterruptedException {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(city).sendKeys(new_city);
}
public void select_gender() throws InterruptedException {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement testDropDown = driver.findElement(gender);
Select dropdown = new Select(testDropDown);
dropdown.selectByValue("MALE");
}
public void skypeid(String sky) throws InterruptedException {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(skype).sendKeys(sky);
}
public void select_img(String img2) throws InterruptedException {
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
System.out.println("Image uploading");
WebElement element = driver.findElement(img);
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
driver.findElement(img).sendKeys(img2);
System.out.println("Image uploaded");
}
public void butn() throws InterruptedException {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(subj).click();
// driver.findElement(date).click();
}
}
文件:2
package Practice.Practice;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import com.worpress.pages.LoginPage;
public class VerifyLogin {
@Test
public void Verify_() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "/home/sheemal/Downloads/ccc/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://dev.blocedu.com/login");
driver.manage().window().maximize();
LoginPage lg = new LoginPage(driver);
lg.enterEmail("student007@yopmail.com");
lg.enterPassword("Testing@123");
lg.clickLogin();
lg.all_cat();
lg.search_cat("Finance");
lg.search_cat1();
lg.change_password();
lg.old_password("Testing@123");
lg.new_password("Testing@123");
lg.confirm_password("Testing@123");
lg.submit();
lg.toaster();
lg.profile();
lg.select_dob();
lg.select_currentdate();
lg.select_previous();
lg.select_year();
lg.select_month();
lg.select_date();
lg.select_city("Delhi");
lg.select_gender();
lg.skypeid("live:rohan");
lg.select_img("\\home\\sheemal\\5.PNG");
lg.butn();
}
}
错误消息在这里: org.openqa.selenium.ElementNotInteractableException:元素不可交互