如何使用python在selenium中逐个颜色找到元素

时间:2021-01-05 19:31:09

This is my case.Two div case are the almost the same, but I need click only "Claim" from first one.Can I use background color to indentify my element? Or any other ideas?

这是我的情况。两个div几乎是相同的,但我需要从第一个单击“声明”。我可以使用背景颜色来识别我的元素吗?还是其他任何想法?

1.

1。

<div class="well well-sm" style="width:150px;margin:5px;text-align:center;float:left;">
  <p><b style="color:#227A11">$0.005</b></p>
  <p><a href="./seecashlinks.php?ocd=open&amp;id=96396" class="btn btn-success" style="background:#0373F1;">Claim</a></p>
</div>

2.

2。

<div class="well well-sm" style="width:150px;margin:5px;text-align:center;float:left;">
  <p><b style="color:#227A11">$0.001</b></p>
  <p><a href="./seecashlinks.php?ocd=open&amp;id=22952" class="btn btn-success" style="background:#CC07DD;">Claim</a></p>
</div>

1 个解决方案

#1


1  

This is how you can identify the div-element that contains an anchor element with the background color you were searching for using xpath:

这是如何使用xpath识别包含您使用xpath搜索的背景颜色的锚元素的div元素:

driver.findElement(By.xpath("//a[@style='background:#0373F1;']/ancestor::div[1]"));

* If there are several anchor-elements with that background color on your site you would have to modify your xpath accordingly (you would need to first search for all "possible" parent elements

*如果您的站点上有多个具有该背景颜色的锚元素,则必须相应地修改您的xpath(您需要首先搜索所有“可能的”父元素

In your case you could for example first search for all div elements that contain class "well":

在您的情况下,您可以首先搜索包含类“well”的所有div元素:

"//div[contains(@class, 'well')]/a[@style='background:#0373F1;']/ancestor::div[1]"

#1


1  

This is how you can identify the div-element that contains an anchor element with the background color you were searching for using xpath:

这是如何使用xpath识别包含您使用xpath搜索的背景颜色的锚元素的div元素:

driver.findElement(By.xpath("//a[@style='background:#0373F1;']/ancestor::div[1]"));

* If there are several anchor-elements with that background color on your site you would have to modify your xpath accordingly (you would need to first search for all "possible" parent elements

*如果您的站点上有多个具有该背景颜色的锚元素,则必须相应地修改您的xpath(您需要首先搜索所有“可能的”父元素

In your case you could for example first search for all div elements that contain class "well":

在您的情况下,您可以首先搜索包含类“well”的所有div元素:

"//div[contains(@class, 'well')]/a[@style='background:#0373F1;']/ancestor::div[1]"