如何使用 xpath 查找元素?
How do I find an element with xpath?
我正在尝试找到一个“按钮”并单击它,但它没有 ID。
我尝试使用 Xpath 甚至 cssSelector 但没有任何效果。
这是按钮的元素:
div style="cursor: grabbing;" title="Approved Forms" onclick="goToForms(0)" class="col-lg-2 col-sm-6"
我试过了:
driver.findElement(By.xpath("//div[contains(@title=,'Approved Forms')]")).click();
driver.findElement(By.xpath("/html/body/div[3]/div/div/div[2]/div[1]")).click(); *copy xpath*
driver.findElement(By.cssSelector(['title="Approved Forms"'])).click(); *has a sintax errors, not sure how to write it*
要定位元素,您可以使用以下任一方法 :
cssSelector
:
WebElement element = driver.findElement(By.cssSelector("div[title='Approved Forms'][onclick^='goToForms']"));
xpath
:
WebElement element = driver.findElement(By.xpath("//div[@title='Approved Forms' and starts-with(@onclick, 'goToForms')]"));
理想情况下,您需要为 visibilityOfElementLocated()
引入 ,您可以使用以下任一 定位器策略 :
cssSelector
:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[title='Approved Forms'][onclick^='goToForms']")));
xpath
:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@title='Approved Forms' and starts-with(@onclick, 'goToForms')]")));
我正在尝试找到一个“按钮”并单击它,但它没有 ID。 我尝试使用 Xpath 甚至 cssSelector 但没有任何效果。
这是按钮的元素:
div style="cursor: grabbing;" title="Approved Forms" onclick="goToForms(0)" class="col-lg-2 col-sm-6"
我试过了:
driver.findElement(By.xpath("//div[contains(@title=,'Approved Forms')]")).click();
driver.findElement(By.xpath("/html/body/div[3]/div/div/div[2]/div[1]")).click(); *copy xpath*
driver.findElement(By.cssSelector(['title="Approved Forms"'])).click(); *has a sintax errors, not sure how to write it*
要定位元素,您可以使用以下任一方法
cssSelector
:WebElement element = driver.findElement(By.cssSelector("div[title='Approved Forms'][onclick^='goToForms']"));
xpath
:WebElement element = driver.findElement(By.xpath("//div[@title='Approved Forms' and starts-with(@onclick, 'goToForms')]"));
理想情况下,您需要为 visibilityOfElementLocated()
引入
cssSelector
:WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[title='Approved Forms'][onclick^='goToForms']")));
xpath
:WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@title='Approved Forms' and starts-with(@onclick, 'goToForms')]")));