我测试的页面上的元素如是否//img或//i有alt属性。
我找不到一种方法来检测该属性何时根本不存在。
这是 WebElement。只是一个没有 alt 属性的 img。
<img class="gsc-branding-img" src="https://www.google.com/cse/static/images/1x/googlelogo_grey_46x15dp.png" srcset="https://www.google.com/cse/static/images/2x/googlelogo_grey_46x15dp.png 2x"/>
Run Code Online (Sandbox Code Playgroud)
这是我试图确定 alt 是否存在的代码。我知道这不是全部必要的,我只是在尝试一切。
WebElement we == driver.findElement(By.xpath("(//img)[1]"));
String altAttribute = we.getAttribute("alt");
if(altAttribute == null || altAttribute =="" || altAttribute == " ")
{
//attribute not found
}
Run Code Online (Sandbox Code Playgroud)
好像是返回一个空字符串...例如下面的代码返回“beforeAfter”
System.out.println("before"+altAttribute+"After");
Run Code Online (Sandbox Code Playgroud)
但是,我的if语句没有赶上返回,所以我不知道该怎么办。
我知道我可以用 (driver.findElements(By.xpath("Xpath Value")).size() != 0);
但是,我使用的是页面对象模型,其全部目的是在单独的类中预定义WebElement,因此不必在测试类中使用“ FindElements By”。
这是我目前拥有的
if (objPage.webElement.isEnabled()){
System.out.println("found element");
}else{
System.out.println("element not found");
}
Run Code Online (Sandbox Code Playgroud)
但是,这试图识别可能不存在的WebElement。当它不存在时,我得到:
没有此类元素”例外。
我使用Maven运行Selenium Testng测试,但是我试图创建可执行jar,因此其他团队成员只需简单的设置即可从其桌面运行自动化套件。
因此,我添加了一个主要的“运行器”方法来创建可执行jar。
public class LaunchTests {
public static void main(String[] args) {
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
List<String> suites = new ArrayList();
suites.add("testng.xml");//path to xml..
testng.setTestSuites(suites);
testng.run();
}
Run Code Online (Sandbox Code Playgroud)
}
我最好的猜测是需要向POM.xml中添加一些内容吗?
从eclipse手动运行testng测试或使用mvn clean测试(Surefire插件)运行它们时,它们可以完美运行。但是当尝试以Java程序运行时出现此错误。
Exception in thread "main" org.testng.TestNGException:
Cannot find class in classpath: package.TestOne
at org.testng.xml.XmlClass.loadClass(XmlClass.java:81)
at org.testng.xml.XmlClass.init(XmlClass.java:73)
at org.testng.xml.XmlClass.<init>(XmlClass.java:59)
at org.testng.xml.TestNGContentHandler.startElement(TestNGContentHandler.java:548)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown …Run Code Online (Sandbox Code Playgroud) 我需要点击"客户一"的链接.目前我正在使用
//div[contains(@class,'client-info') and contains(div/text(),'Customer')]
Run Code Online (Sandbox Code Playgroud)
使用Firepath,这将返回四个单独的元素(我附加了包含其中两个的代码),我认为这就是为什么Selenium Webdriver无法找到我想要单击的元素.
我需要单击此xpath的最后一个实例,但它似乎不可能更具体,因为它们具有相同的类和内部文本!
<div class="wg-client-row-mobile hidden-sm hidden-md hidden-lg">
<div class="row">
<div class="col-xs-11">
<span class="icon icon-user" ng-class="{'icon-user': !wgClientItemCtrl.client.isAnOrganization, 'icon-business': wgClientItemCtrl.client.isAnOrganization}"/>
<div class="client-info">
<div class="client-name not-long-text ng-binding">Customer One</div>
<!-- <div class="client-age not-long-text" ng-if="!wgClientItemCtrl.client.isAnOrganization">{{wgClientItemCtrl.client.gender}}, {{wgClientItemCtrl.client.calculatedAge | notAvailable}}</div> -->
<!-- ngIf: !wgClientItemCtrl.client.isAnOrganization -->
<div class="client-age not-long-text ng-binding ng-scope" ng-if="!wgClientItemCtrl.client.isAnOrganization">
<!-- end ngIf: !wgClientItemCtrl.client.isAnOrganization -->
<!-- ngIf: wgClientItemCtrl.client.isAnOrganization -->
<div class="client-age not-long-text ng-binding">
</div>
</div>
<div class="favorite-right-container">
</div>
</div>
<div class="wg-client-row-desktop hidden-xs hidden-is">
<div class="row content">
<div class="col-sm-4">
<div class="icon icon-user" …Run Code Online (Sandbox Code Playgroud) 我有一个响应式设计,我需要与之交互的很多项目都有2个WebElements.一个用于桌面,一个用于移动,我正在尝试使用PageFactory.以下是我现在要识别并与元素交互的内容.
//this returns 2 webelements, one for desktop and one for mobile
@FindBy(xpath = "//selector-dropdown/p")
private WebElement dropdown;
public void ClickDropdown() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOf(dropdown)).click();
}
Run Code Online (Sandbox Code Playgroud)
我的印象是ExpectedConditions.visibilityOf(WebElement)会发现第一个元素可见.现在,当我在桌面上打开应用程序时,它会找到该元素(桌面是DOM中的第一个).但是在移动设备上,它等待可见性超时,因为它等待第一个变得可见.
我的另一种方法是使用@FindBy来声明每个元素两次,然后创建一个if语句来决定采用哪个路径.这项额外的工作是否能使其发挥作用?