java Selenium如何单击列表中的每个相同按钮并提交表单
有问题的网址https://www.doctorxdentist.com/find-a-doctor
我希望执行以下步骤
- 筛选列表 1.1)单击药物区域 1.2)单击“牙科” 1.3)单击“专业” 1.4)单击“牙医”
- 单击获取报价按钮
- 填表
- 重复,但单击下一个获取报价按钮
这是到目前为止我的代码。我希望按钮不是唯一的,我可以使用xpath的减量来获取每个get quote按钮,但不幸的是,它们的xpath都是相同的
另外,我最初的点击语句似乎没有达到预期效果。如果你能检查一下,那就太好了
package Script;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class DoctorXDentist {
public static boolean isClickable(WebElement webe, WebDriver browser){
try
{
WebDriverWait wait = new WebDriverWait(browser, 5);
wait.until(ExpectedConditions.elementToBeClickable(webe));
return true;
}
catch (Exception e)
{
return false;
}
}
public static void initiate() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\jinyi\\Downloads\\chromedriver_win32 (2)\\chromedriver.exe");
/*
* ChromeOptions options = new ChromeOptions();
* options.setHeadless(true);
*/
WebDriver browser = new ChromeDriver();
browser.manage().window().maximize();
browser.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
browser.get("https://www.doctorxdentist.com/find-a-doctor");
Actions action = new Actions(browser);
// xpth -> go to clickable, copy xpath
// Click Area of Medicine
action.moveToElement(browser.findElement(By.xpath("//*[@id=\"app\"]/div/div[1]/div/div[2]/div/form/div[4]/div/div[1]/div"))).sendKeys(Keys.RETURN);
WebElement element = browser.findElement(By.xpath("//*[@id=\\\"app\\\"]/div/div[1]/div/div[2]/div/form/div[4]/div/div[1]/div"));
isClickable(element, browser);
// Click Dental
action.moveToElement(browser.findElement(By.xpath("//*[@id=\"select\"]/div/div[6]/label/span[2]"))).click();
// Click specialty
action.moveToElement(browser.findElement(By.xpath("//*[@id=\"app\"]/div/div[1]/div/div[2]/div/form/div[3]/div/div[1]/div"))).click();
// Click Dentist
action.moveToElement(browser.findElement(By.xpath("//*[@id=\"select\"]/div/div[3]/label/span[2]"))).click();
List<WebElement> box = null;
for ( int i = 1; i < 200; i++) {
box.add((WebElement) browser.findElements(By.xpath("//*[@class='box'][i]//*[@class='doctor']//button[text()='Get Quote']")));
System.out.println(box);
}
// after get quote function completed
// click on form
// action.moveToElement(browser.findElement(By.xpath("/html/body/div[5]/div[2]/div/section/div/div[2]/section/div[1]/div[1]/div/input"))).click();
}
public static void main(String[] args) {
initiate();
}
}
我也收到了下面的信息,它没有说任何错误,我也不知道它的意思
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 83.0.4103.116, chrome: {chromedriverVersion: 83.0.4103.39 (ccbf011cb2d2b..., userDataDir: C:\Users\jinyi\AppData\Loca...}, goog:chromeOptions: {debuggerAddress: localhost:59331}, 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: 12f47c7e9735f47822adfbedf2dd3619
*** Element info: {Using=xpath, value=//*[@id=\"app\"]/div/div[1]/div/div[2]/div/form/div[4]/div/div[1]/div}
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:196)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:129)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:161)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:582)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:333)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:451)
at org.openqa.selenium.By$ByXPath.findElement(By.java:394)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:325)
at Script.DoctorXDentist.initiate(DoctorXDentist.java:49)
at Script.DoctorXDentist.main(DoctorXDentist.java:68)
# 1 楼答案
这个代码对我来说很好用。如果这有帮助,请标记为答案。 优化代码