【发布时间】:2020-07-06 01:19:54
【问题描述】:
有问题的网址https://www.doctorxdentist.com/find-a-doctor
我希望执行以下步骤
- 过滤器列表 1.1) 点击医学区域 1.2) 点击牙科 1.3)点击专业 1.4) 点击牙医
- 点击获取报价按钮
- 填写表格
- 重复但点击下一个获取报价按钮
到目前为止,这是我的代码。我希望这些按钮不是唯一的,我可以使用 xpath 的减量来获取每个获取报价按钮,但不幸的是它们的 xpath 都是一样的。
P.S 似乎我最初的点击语句没有按预期工作。如果您可以检查它们,那就太好了
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)
【问题讨论】:
-
嗨,你能帮帮我吗,你指的是哪个按钮-“不幸的是,他们的 xpath 都是一样的”
-
@DurgaPrasadBehera 这是获取报价按钮
-
试试这个 - //*[@class='doctor']//button[text()='Get Quote']
-
我可以得到按钮,但我需要它在提交每个表单后点击下面的按钮,你也可以检查我之前的点击语句是否有效?感谢您的帮助!
-
这是给出唯一数据- //*[@class='box'][1]//*[@class='doctor']//button[text()='Get Quote ']
标签: java selenium web-scraping