无法从 Selenium 中的日期选择器获取 select 日期
Unable to select date from date picker in Selenium
package newpackage;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Example {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","D:\downloads\chromedriver\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://demoqa.com/automation-practice-form");
try {
String fname="abc";
String lname="xyz";
String email="abcxyz@gmail.com";
driver.findElement(By.id("firstName")).sendKeys(fname);
Thread.sleep(1000);
driver.findElement(By.id("lastName")).sendKeys(lname);
Thread.sleep(1000);
driver.findElement(By.id("userEmail")).sendKeys(email);
Thread.sleep(1000);
WebElement element = driver.findElement(By.id("gender-radio-1"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();",element);
Thread.sleep(2000);
Thread.sleep(1000);
driver.findElement(By.id("userNumber")).sendKeys("909876765");
Thread.sleep(1000);
String date ="13-August-2019";
String dateArr[]= date.split("-");
String day=dateArr[0];
String month=dateArr[1];
String year=dateArr[2];
driver.findElement(By.id("dateOfBirthInput")).click();
Select selectMonth =new Select(driver.findElement(By.xpath("//*[@id=\"dateOfBirth\"]/div[2]/div[2]/div/div/div[2]/div[1]/div[2]/div[1]/select")));
//System.out.println(driver.findElement(By.xpath("//*[@id=\"dateOfBirth\"]/div[2]/div[2]/div/div/div[2]/div[1]/div[2]/div[1]")).getText());
selectMonth.selectByVisibleText(month);
Select selectYear=new Select(driver.findElement(By.className("react-datepicker__year-select")));
selectYear.selectByVisibleText(year);
String beforePath="//*[@id=\"dateOfBirth\"]/div[2]/div[2]/div/div/div[2]/div[2]/div[";
String afterPath="]/div[";
boolean flag=false;
for (int row =1 ;row <=6; row++) {
for (int col =1; col<=7; col++) {
System.out.println(driver.findElement(By.xpath(beforePath+row+afterPath+col+"]")).getText());
if(driver.findElement(By.xpath(beforePath+row+afterPath+col+"]")).getText().equals(day)) {
driver.findElement(By.xpath(beforePath+row+afterPath+col+"]")).click()
flag=true;
break;
}
if(flag)
break;
}
}
Thread.sleep(1000);
driver.findElement(By.id("subjectsInput")).sendKeys("English");
Thread.sleep(1000);
Thread.sleep(1000);
driver.findElement(By.id("submit")).submit();
}
finally{
driver.close();
}
}
}
Link of for used for avove code : https://demoqa.com/automation-practice-form
我收到以下错误:
Starting ChromeDriver 84.0.4147.30 (48b3e868b4cc0aa7e8149519690b6f6949e110a8-refs/branch-heads/4147@{#310}) on port 31214
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Aug 06, 2020 7:22:11 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
************************
*************************************************************
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="dateOfBirth"]/div[2]/div[2]/div/div/div[2]/div[2]/div[4]/div[1]"}
(Session info: chrome=84.0.4147.105)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'DESKTOP-E926NDJ', ip: '192.168.178.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_181'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 84.0.4147.105, chrome: {chromedriverVersion: 84.0.4147.30 (48b3e868b4cc0..., userDataDir: C:\Users\hp\AppData\Local\T...}, goog:chromeOptions: {debuggerAddress: localhost:64230}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true}
Session ID: dd62b7398d8b7df16f67bd9ccedc36d4
*** Element info: {Using=xpath, value=//*[@id="dateOfBirth"]/div[2]/div[2]/div/div/div[2]/div[2]/div[4]/div[1]}
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)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
at newpackage.Example.main(Example.java:68)
我无法select日期
我面临的错误是:线程“main”中的异常 org.openqa.selenium.NoSuchElementException:没有这样的元素:无法定位元素:{“方法”:“xpath”,“select 或”: "//*[@id="dateOfBirth"]/div[2]/div[2]/div/div/div[2]/div[2]/div[4]/div[1]"}
我能够重现问题,请使用以下代码修复它 -
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./src/main/resources/drivers/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://demoqa.com/automation-practice-form");
try {
String fname = "abc";
String lname = "xyz";
String email = "abcxyz@gmail.com";
driver.findElement(By.id("firstName")).sendKeys(fname);
Thread.sleep(1000);
driver.findElement(By.id("lastName")).sendKeys(lname);
Thread.sleep(1000);
driver.findElement(By.id("userEmail")).sendKeys(email);
Thread.sleep(1000);
WebElement element = driver.findElement(By.id("gender-radio-1"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Thread.sleep(2000);
Thread.sleep(1000);
driver.findElement(By.id("userNumber")).sendKeys("909876765");
Thread.sleep(1000);
String date = "13-August-2019";
String dateArr[] = date.split("-");
String day = dateArr[0];
String month = dateArr[1];
String year = dateArr[2];
driver.findElement(By.id("dateOfBirthInput")).click();
Select selectMonth = new Select(driver.findElement(By.xpath("//*[@id=\"dateOfBirth\"]/div[2]/div[2]/div/div/div[2]/div[1]/div[2]/div[1]/select")));
// System.out.println(driver.findElement(By.xpath("//*[@id=\"dateOfBirth\"]/div[2]/div[2]/div/div/div[2]/div[1]/div[2]/div[1]")).getText());
selectMonth.selectByVisibleText(month);
Select selectYear = new Select(driver.findElement(By.className("react-datepicker__year-select")));
selectYear.selectByVisibleText(year);
String beforePath = "//div[@id='dateOfBirth']/div[2]/div[2]/div/div/div[2]/div[2]/div[";
String afterPath = "]/div[";
boolean flag = false;
for (int row = 1; row <= 6; row++) {
for (int col = 1; col <= 7; col++) {
System.out.println("Row: " + row);
System.out.println("Col: " + col);
System.out.println(driver.findElement(By.xpath(beforePath + row + afterPath + col + "]")).getText());
if (driver.findElement(By.xpath(beforePath + row + afterPath + col + "]")).getText().equals(day)) {
driver.findElement(By.xpath(beforePath + row + afterPath + col + "]")).click();
flag = true;
break;
}
}
if (flag)
break;
}
Thread.sleep(1000);
driver.findElement(By.id("subjectsInput")).sendKeys("English");
Thread.sleep(1000);
Thread.sleep(1000);
driver.findElement(By.id("submit")).submit();
} catch (Exception e) {
e.printStackTrace();
}
finally {
driver.close();
}
}
问题在于第二个 for 循环的 break
语句的放置。
此外,在下一行的末尾缺少 semi-colon -
driver.findElement(By.xpath(beforePath+row+afterPath+col+"]")).click()
package newpackage;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Example {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","D:\downloads\chromedriver\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://demoqa.com/automation-practice-form");
try {
String fname="abc";
String lname="xyz";
String email="abcxyz@gmail.com";
driver.findElement(By.id("firstName")).sendKeys(fname);
Thread.sleep(1000);
driver.findElement(By.id("lastName")).sendKeys(lname);
Thread.sleep(1000);
driver.findElement(By.id("userEmail")).sendKeys(email);
Thread.sleep(1000);
WebElement element = driver.findElement(By.id("gender-radio-1"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();",element);
Thread.sleep(2000);
Thread.sleep(1000);
driver.findElement(By.id("userNumber")).sendKeys("909876765");
Thread.sleep(1000);
String date ="13-August-2019";
String dateArr[]= date.split("-");
String day=dateArr[0];
String month=dateArr[1];
String year=dateArr[2];
driver.findElement(By.id("dateOfBirthInput")).click();
Select selectMonth =new Select(driver.findElement(By.xpath("//*[@id=\"dateOfBirth\"]/div[2]/div[2]/div/div/div[2]/div[1]/div[2]/div[1]/select")));
//System.out.println(driver.findElement(By.xpath("//*[@id=\"dateOfBirth\"]/div[2]/div[2]/div/div/div[2]/div[1]/div[2]/div[1]")).getText());
selectMonth.selectByVisibleText(month);
Select selectYear=new Select(driver.findElement(By.className("react-datepicker__year-select")));
selectYear.selectByVisibleText(year);
String beforePath="//*[@id=\"dateOfBirth\"]/div[2]/div[2]/div/div/div[2]/div[2]/div[";
String afterPath="]/div[";
boolean flag=false;
for (int row =1 ;row <=6; row++) {
for (int col =1; col<=7; col++) {
System.out.println(driver.findElement(By.xpath(beforePath+row+afterPath+col+"]")).getText());
if(driver.findElement(By.xpath(beforePath+row+afterPath+col+"]")).getText().equals(day)) {
driver.findElement(By.xpath(beforePath+row+afterPath+col+"]")).click()
flag=true;
break;
}
if(flag)
break;
}
}
Thread.sleep(1000);
driver.findElement(By.id("subjectsInput")).sendKeys("English");
Thread.sleep(1000);
Thread.sleep(1000);
driver.findElement(By.id("submit")).submit();
}
finally{
driver.close();
}
}
}
Link of for used for avove code : https://demoqa.com/automation-practice-form
我收到以下错误:
Starting ChromeDriver 84.0.4147.30 (48b3e868b4cc0aa7e8149519690b6f6949e110a8-refs/branch-heads/4147@{#310}) on port 31214
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Aug 06, 2020 7:22:11 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
************************
*************************************************************
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="dateOfBirth"]/div[2]/div[2]/div/div/div[2]/div[2]/div[4]/div[1]"}
(Session info: chrome=84.0.4147.105)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'DESKTOP-E926NDJ', ip: '192.168.178.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_181'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 84.0.4147.105, chrome: {chromedriverVersion: 84.0.4147.30 (48b3e868b4cc0..., userDataDir: C:\Users\hp\AppData\Local\T...}, goog:chromeOptions: {debuggerAddress: localhost:64230}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true}
Session ID: dd62b7398d8b7df16f67bd9ccedc36d4
*** Element info: {Using=xpath, value=//*[@id="dateOfBirth"]/div[2]/div[2]/div/div/div[2]/div[2]/div[4]/div[1]}
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)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
at newpackage.Example.main(Example.java:68)
我无法select日期
我面临的错误是:线程“main”中的异常 org.openqa.selenium.NoSuchElementException:没有这样的元素:无法定位元素:{“方法”:“xpath”,“select 或”: "//*[@id="dateOfBirth"]/div[2]/div[2]/div/div/div[2]/div[2]/div[4]/div[1]"}
我能够重现问题,请使用以下代码修复它 -
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./src/main/resources/drivers/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://demoqa.com/automation-practice-form");
try {
String fname = "abc";
String lname = "xyz";
String email = "abcxyz@gmail.com";
driver.findElement(By.id("firstName")).sendKeys(fname);
Thread.sleep(1000);
driver.findElement(By.id("lastName")).sendKeys(lname);
Thread.sleep(1000);
driver.findElement(By.id("userEmail")).sendKeys(email);
Thread.sleep(1000);
WebElement element = driver.findElement(By.id("gender-radio-1"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Thread.sleep(2000);
Thread.sleep(1000);
driver.findElement(By.id("userNumber")).sendKeys("909876765");
Thread.sleep(1000);
String date = "13-August-2019";
String dateArr[] = date.split("-");
String day = dateArr[0];
String month = dateArr[1];
String year = dateArr[2];
driver.findElement(By.id("dateOfBirthInput")).click();
Select selectMonth = new Select(driver.findElement(By.xpath("//*[@id=\"dateOfBirth\"]/div[2]/div[2]/div/div/div[2]/div[1]/div[2]/div[1]/select")));
// System.out.println(driver.findElement(By.xpath("//*[@id=\"dateOfBirth\"]/div[2]/div[2]/div/div/div[2]/div[1]/div[2]/div[1]")).getText());
selectMonth.selectByVisibleText(month);
Select selectYear = new Select(driver.findElement(By.className("react-datepicker__year-select")));
selectYear.selectByVisibleText(year);
String beforePath = "//div[@id='dateOfBirth']/div[2]/div[2]/div/div/div[2]/div[2]/div[";
String afterPath = "]/div[";
boolean flag = false;
for (int row = 1; row <= 6; row++) {
for (int col = 1; col <= 7; col++) {
System.out.println("Row: " + row);
System.out.println("Col: " + col);
System.out.println(driver.findElement(By.xpath(beforePath + row + afterPath + col + "]")).getText());
if (driver.findElement(By.xpath(beforePath + row + afterPath + col + "]")).getText().equals(day)) {
driver.findElement(By.xpath(beforePath + row + afterPath + col + "]")).click();
flag = true;
break;
}
}
if (flag)
break;
}
Thread.sleep(1000);
driver.findElement(By.id("subjectsInput")).sendKeys("English");
Thread.sleep(1000);
Thread.sleep(1000);
driver.findElement(By.id("submit")).submit();
} catch (Exception e) {
e.printStackTrace();
}
finally {
driver.close();
}
}
问题在于第二个 for 循环的 break
语句的放置。
此外,在下一行的末尾缺少 semi-colon -
driver.findElement(By.xpath(beforePath+row+afterPath+col+"]")).click()