【问题标题】:How to click on the svg based Delete icon using Selenium and Java如何使用 Selenium 和 Java 单击基于 svg 的删除图标
【发布时间】:2020-07-17 12:22:08
【问题描述】:
我有网络应用程序,我可以登录并查看交易表。有图标,我正在尝试使用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();
【问题讨论】:
标签:
java
selenium
selenium-webdriver
xpath
webdriverwait
【解决方案1】:
要单击启用Delete 的svg 图标,您需要将WebDriverWait 用于elementToBeClickable(),您可以使用以下基于xpath 的Locator Strategies:
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();
}
}
浏览器快照: