当运行代码时,即使我检查站点上的元素,XPath似乎找到了正确的按钮, Aprobă
按钮的XPath也不会点击它 .
错误是:
org.openqa.selenium.NoSuchElementException:没有这样的元素:无法定位元素{“method”:“xpath”,“selector”:“// a [contains( . ,'')] / ancestor :: div [contains (@class,'js-answer-element')] // span [@ class ='sg-icon-as-button__hole']“}
我的方法 approveAnswerByUsername(String username)
应检查答案是否已获批准 . 通过用户名附近的3个按钮和其下方的绿色圆圈识别已批准的答案 . 用户可以是第一个回答,第二个或唯一一个 . 这就是我在XPath中使用"ancestor"的原因 .
在某些情况下,圈子已被看到(即使在先例的情况下),而在其他情况下则没有 . 与按钮 "Aprobă"
相同 .
下面的代码应该执行以下操作:1 . 连接到www.brainly.ro并使用凭据登录2.打开用户Lola1511的窗口3.打开所有已回答的问题并检查是否已批准 . 如果未获批准,则批准 .
下面我发布我的整个代码(网站的链接和凭据都在那里)
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class classTest {
WebDriver driver;
@BeforeTest
public void invokeBrowser() {
try {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\sanduc\\Desktop\\Selenium\\Kits\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.get("");
} catch (Exception e) {
e.printStackTrace();
}
}
@Test(priority = 1)
public void log_in() {
try {
driver.findElement(
By.xpath("//div[@class='sg-content-box__actions']/nav[@class='brn-hero__navigation']/a[1]"))
.click();
driver.findElement(By.xpath("//form[@action='/login?entry=2&return=/']/div[2]/input"))
.sendKeys("my_emal@mail.com");
driver.findElement(By.xpath("//form[@action='/login?entry=2&return=/']/div[3]/input"))
.sendKeys("my_password");
driver.findElement(By.xpath("//button[@type='submit']")).click();
Thread.sleep(4000);
driver.get("");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test(priority = 2)
public void click_pages() {
try {
int index_while = 1;
while (true) {
driver.get("" + index_while);
List<WebElement> demovar = driver.findElements(By.xpath("//div[@class='task-content']/a"));
System.out.println(demovar.size());
System.out.println("+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+");
System.out.println("Current page: " + driver.getCurrentUrl());
System.out.println("+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+");
ArrayList<String> hrefs = new ArrayList<String>(); // List for
// storing
// all href
// values
// for 'a'
// tag
for (WebElement var : demovar) {
System.out.println(var.getText()); // used to get text
// present between the
// anchor tags
System.out.println(var.getAttribute("href"));
hrefs.add(var.getAttribute("href"));
System.out.println("*************************************");
}
// Navigating to each link
int i = 0;
for (String href : hrefs) {
driver.navigate().to(href);
System.out.println((++i) + ": navigated to URL with href: " + href);
approveAnswerByUsername("Lola1511");
Thread.sleep(3000); // To check if the navigation is
// happening properly.
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
}
index_while++;
if (demovar.size() < 5) {
break;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void approveAnswerByUsername(String username) {
try {
if (driver.findElement(By.xpath("//a[contains(.,'" + username
+ "')]/ancestor::div[contains(@class, 'js-answer-element')]//span[@class='sg-icon-as-button__hole']")).isDisplayed()) {
System.out.println("The homework " + driver.getCurrentUrl() + " has ALREADY been approved");
} else {
driver.findElement(By.xpath("//a[contains(.,'" + username
+ "')]/ancestor::div[contains(@class, 'js-answer-element')]//div[contains(@class,'js-approve-button-text')][contains(.,'Aprobă')]")).click();
Thread.sleep(2000);
driver.navigate().refresh();
System.out.println("The homework " + driver.getCurrentUrl() + " has been approved NOW");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
1 回答
由于时间限制,我无法检查代码,但我建议使用不同的方法来使用选择器 .
Use ID selectors or name selectors in the webpage instead of xpaths.
ID选择器是选择元素的最佳和最快的方式 . Xpath较慢,有时不可靠 .
我的建议是尝试ID和名称选择器,并且只有在找不到两者时才使用xpath .