ADADBA
ADADBA

Reputation: 33

How to find element by long complicated xpath?

I want to find a button, but don't know how as the html code is complicated. Can you help me?

HTML

<div style="margin-top: 20px;">
  <span style="display: inline-block; margin: 5px; padding: 10px 15px; border-radius: 5px; font-size: 18px; line-height: 16px; cursor: pointer; color: rgb(255, 255, 255); background-color: rgb(99, 99, 102);">Bugsnag akzeptieren
  </span>
  <span style="display: inline-block; margin: 5px; padding: 10px 15px; border-radius: 5px; font-size: 18px; line-height: 16px; cursor: pointer; color: rgb(255, 255, 255); background-color: rgb(0, 122, 255);">Alle akzeptieren
  </span>
</div>

So I want to find the second element and tried things like:

self.browser.find_elements_by_xpath("//span[@style='the whole text that equals style in the html']")

But it does not seem to work...

Upvotes: 0

Views: 198

Answers (1)

Zach
Zach

Reputation: 930

Depending on what you would like to do with it, you could do //span[2] to select the second span:

Example of Selecting Element

self.browser.find_elements_by_xpath("//span[2]") enter image description here

if you want the entire element.


Example of Selecting Text

self.browser.find_elements_by_xpath("//span[2]/text()")

if you want the inner html text of the second span.

I tested the xpath expression on this website to make sure that it works.

I copied in the html you provided and just entered the xpath expression //span[2]/text() to test it. enter image description here

More info on xpath here.

Upvotes: 1

Related Questions