How can I identify an XPath without regard to div indices?

Viewed 37

I'm writing a python code that scrapes info from a site, but I have to get rid of some cookies pop-ups first. To click the right buttons, I need their XPaths (as far as I know). The problem is that a part of the XPath changes every time, and I don't know how to find them as they don't really have any attributes like an ID or something.

This is the HTML of the button:

<button class="Button__StyledButton-a1qza5-0 lcqSKB" style="visibility: visible; background-color: rgb(12, 44, 91);">Lees meer om voorkeuren te accepteren</button>

This is the command I'm using right now:

driver.find_element_by_xpath('/html/body/div[14]/div[1]/div[3]/button').click()

And this is the XPath:

/html/body/div[VARIABLE]/div[1]/div[3]/button

where VARIABLE changes every time, so I changed the 14.

2 Answers

This XPath,

//button[.='Lees meer om voorkeuren te accepteren']

will select all buttons whose string value is 'Lees meer om voorkeuren te accepteren', independent of layout or position in the DOM hierarchy.

You can make it even more robust by comparing the white-space-normalized string value:

//button[normalize-space()='Lees meer om voorkeuren te accepteren']

That will test against string values after removing leading and trailing whitespace and collapsing multiple contiguous internal whitespace to single spaces.

If you use the full xPath like you are doing you will run into issues like that all the time. The better approach is to figure out another/shorter way to get to that element.

It is hard to test without the URL but try one of these:

driver.find_element_by_css_selector("Button__StyledButton-a1qza5-0.lcqSKB")
driver.find_element_by_css_selector("Button__StyledButton-a1qza5-0")
Related