如何使用 Selenium 和 Java 单击基于 svg 的删除图标
How to click on the svg based Delete icon using Selenium and Java
我有 Web 应用程序,我可以登录并查看 table 笔交易。有图标,我正在尝试使用webdriver获取图标,我无法获取图标。
如何点击删除图标和动作触发?
String baseURL = "http://testingapp.workspez.com/login";
driver.get(baseURL);
driver.findElement(By.xpath("//input[@id='field_email']")).sendKeys("rahul@workspez.com");
driver.findElement(By.xpath("//input[@id='field_password']")).sendKeys("Sujeet@19");
driver.findElement(By.className("MuiButton-label")).click();
WebElement generalInfo = driver.findElement(By.xpath("//*[text()='General Info']"));
generalInfo.click();
WebElement md = driver.findElement(By.xpath("//*[text()='Contacts']"));
md.click();
WebElement searchbox = driver.findElement(By.xpath("//input[@class='MuiInputBase-input MuiInput-input']"));
searchbox.sendKeys("fat1");
WebElement search = driver.findElement(By.xpath(".//*[@class='MuiButtonBase-root MuiIconButton-root']"));
search.click();
List<WebElement> menulist = driver.findElements(By.xpath(".//*[@class='MuiButtonBase-root MuiIconButton-root']"));
System.out.println(menulist.size());
menulist.get(3).click();
WebElement deleteicon = driver.findElement(By.xpath("//ul[@class='MuiList-root MuiMenu-list MuiList-padding']"));
deleteicon.click();
点击svg enabled Delete
icon you need to use for the elementToBeClickable()
and you can use the following xpath based :
import java.util.Collections;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ElementToBeClickable {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\WebDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.setExperimentalOption("useAutomationExtension", false);
WebDriver driver = new ChromeDriver(options);
driver.get("http://testingapp.workspez.com/login");
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='field_email']"))).sendKeys("rahul@workspez.com");
driver.findElement(By.xpath("//input[@id='field_password']")).sendKeys("Sujeet@19");
driver.findElement(By.xpath("//span[@class='MuiButton-label' and contains(., 'Log In')]")).click();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='MuiButton-label' and contains(., 'General Info')]"))).click();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='MuiInputBase-input MuiInput-input']"))).sendKeys("aa");
driver.findElement(By.xpath("//button[@class='MuiButtonBase-root MuiIconButton-root' and @title='Search']")).click();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[@class='MuiTableCell-root MuiTableCell-body']//button[@class='MuiButtonBase-root MuiIconButton-root']/span[@class='MuiIconButton-label']"))).click();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@class='MuiButtonBase-root MuiListItem-root MuiMenuItem-root MuiMenuItem-gutters MuiListItem-gutters MuiListItem-button']/*[name()='svg']"))).click();
}
}
浏览器快照:
我有 Web 应用程序,我可以登录并查看 table 笔交易。有图标,我正在尝试使用webdriver获取图标,我无法获取图标。
如何点击删除图标和动作触发?
String baseURL = "http://testingapp.workspez.com/login";
driver.get(baseURL);
driver.findElement(By.xpath("//input[@id='field_email']")).sendKeys("rahul@workspez.com");
driver.findElement(By.xpath("//input[@id='field_password']")).sendKeys("Sujeet@19");
driver.findElement(By.className("MuiButton-label")).click();
WebElement generalInfo = driver.findElement(By.xpath("//*[text()='General Info']"));
generalInfo.click();
WebElement md = driver.findElement(By.xpath("//*[text()='Contacts']"));
md.click();
WebElement searchbox = driver.findElement(By.xpath("//input[@class='MuiInputBase-input MuiInput-input']"));
searchbox.sendKeys("fat1");
WebElement search = driver.findElement(By.xpath(".//*[@class='MuiButtonBase-root MuiIconButton-root']"));
search.click();
List<WebElement> menulist = driver.findElements(By.xpath(".//*[@class='MuiButtonBase-root MuiIconButton-root']"));
System.out.println(menulist.size());
menulist.get(3).click();
WebElement deleteicon = driver.findElement(By.xpath("//ul[@class='MuiList-root MuiMenu-list MuiList-padding']"));
deleteicon.click();
点击svg enabled Delete
icon you need to use elementToBeClickable()
and you can use the following xpath based
import java.util.Collections;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ElementToBeClickable {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\WebDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.setExperimentalOption("useAutomationExtension", false);
WebDriver driver = new ChromeDriver(options);
driver.get("http://testingapp.workspez.com/login");
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='field_email']"))).sendKeys("rahul@workspez.com");
driver.findElement(By.xpath("//input[@id='field_password']")).sendKeys("Sujeet@19");
driver.findElement(By.xpath("//span[@class='MuiButton-label' and contains(., 'Log In')]")).click();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='MuiButton-label' and contains(., 'General Info')]"))).click();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='MuiInputBase-input MuiInput-input']"))).sendKeys("aa");
driver.findElement(By.xpath("//button[@class='MuiButtonBase-root MuiIconButton-root' and @title='Search']")).click();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[@class='MuiTableCell-root MuiTableCell-body']//button[@class='MuiButtonBase-root MuiIconButton-root']/span[@class='MuiIconButton-label']"))).click();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@class='MuiButtonBase-root MuiListItem-root MuiMenuItem-root MuiMenuItem-gutters MuiListItem-gutters MuiListItem-button']/*[name()='svg']"))).click();
}
}
浏览器快照: