在页面上查找元素

huangapple go评论64阅读模式
英文:

FInd element on page

问题

我有一个 此页面

页面上有共享元素(帖子左下方):

<button class="btn btn_share" role="dropdown_trigger" data-toggle="dropdown" type="button" title=" Поделиться вопросом" aria-expanded="false">
  <svg class="icon_svg icon_sharing" viewBox="0 0 32 32">
    <use href="5e6a141a/images/sprite.svg#icon_sharing"></use>
  </svg>
</button>

尝试使用 [标签:xpath],

List<WebElement> elements=driver.findElements(By.xpath("xpath=//div[@class='btn btn_share']/div/div[5]/div/button"));

if (elements == null) {
    System.out.println("找不到分享按钮");
    System.exit(1);
} else {
    for (WebElement element : elements) {
        element.click();
    }
}

和使用类选择器。

List<WebElement> elements=driver.findElementsByClassName("btn_share");

if (elements == null) {
    System.out.println("找不到分享按钮");
    System.exit(1);
} else {
    for (WebElement element : elements) {
        element.click();
        TimeUnit.SECONDS.sleep(50);
    }
}

但没有任何结果。

如何选择一个元素并点击?

英文:

I have a this page,

On page is elements share (left-down on post):

<button class="btn btn_share" role="dropdown_trigger" data-toggle="dropdown" type="button" title=" Поделиться вопросом" aria-expanded="false">
  <svg class="icon_svg icon_sharing" viewBox="0 0 32 32">
    <use href="5e6a141a/images/sprite.svg#icon_sharing"></use>
  </svg>
</button>

Tried using [tags:xpath],

List<WebElement> elements=driver.findElements(By.xpath("xpath=//div[@class='btn btn_share']/div/div[5]/div/button"));

if (elements == null) {
    System.out.println("sharing no found");
    System.exit(1);
} else {
    for (WebElement element : elements) {
        element.click();
    }
}

and using class select.

List<WebElement> elements=driver.findElementsByClassName("btn_share");

if (elements == null) {
    System.out.println("sharing no found");
    System.exit(1);
} else {
    for (WebElement element : elements) {
        element.click();
        TimeUnit.SECONDS.sleep(50);
    }
}

But no any result.

How can I select one element and click?

答案1

得分: 1

使用 By.cssSelector 和这个值来查找元素:[class*='btn_share']

而且在验证时,你可以使用 elements.size()==0 而不是 elements == null

List<WebElement> elements = driver.findElements(By.cssSelector("[class*='btn_share']"));

if (elements.size() == 0) {
    System.out.println("未找到分享按钮");
} else {
    for (WebElement element : elements) {
        element.click();
    }
}

CSS 选择器参考

英文:

Use find elements By.cssSelector with this value : [class*=&#39;btn_share&#39;].

And for the validation you can use elements.size()==0 not elements == null.

List&lt;WebElement&gt; elements = driver.findElements(By.cssSelector(&quot;[class*=&#39;btn_share&#39;]&quot;));
		
if(elements.size()==0) {
	System.out.println(&quot;sharing no found&quot;);
}else {
	for(WebElement element: elements) {
		element.click();
	}
}

CSS Selector Reference

huangapple
  • 本文由 发表于 2020年3月16日 01:45:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/60695862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定