如何识别XPath而不使用索引?

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

how do i identify an xpath without indices

问题

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.

英文:

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:

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

This is the command I'm using right now:

driver.find_element_by_xpath(&#39;/html/body/div[14]/div[1]/div[3]/button&#39;).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.

答案1

得分: 2

以下是翻译好的部分:

你可以使用一些通用的 XPath:

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

或者

/*/button[contains(@class, 'Button__StyledButton-a1qza5-0')]

结果是:

driver.find_element_by_xpath('/*/button[@class="Button__StyledButton-a1qza5-0 lcqSKB"]').click()

这种方法有助于识别元素(按钮),无论完整 XPath 中的变量如何。

有关 XPath 语法的更多信息,请参见:
W3schools-xpath

英文:

You could use some generic xpath:

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

/*/button[@class=&quot;Button__StyledButton-a1qza5-0 lcqSKB&quot;]

or

/*/button[contains(@class, &#39;Button__StyledButton-a1qza5-0&#39;)]

results in:

driver.find_element_by_xpath(&#39;/*/button[@class=&quot;Button__StyledButton-a1qza5-0 lcqSKB&quot;]&#39;).click()

This approach helps identifying the element (button) no matter the variable in the full xpath.

For more infos regarding xpath syntax see:
W3schools-xpath

答案2

得分: 0

如果您一直使用完整的 xPath,就会经常遇到类似的问题。更好的方法是找到另一种/更短的方法来定位该元素。

很难在没有URL的情况下进行测试,但可以尝试以下之一:

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

英文:

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(&quot;Button__StyledButton-a1qza5-0.lcqSKB&quot;)
driver.find_element_by_css_selector(&quot;Button__StyledButton-a1qza5-0&quot;)

huangapple
  • 本文由 发表于 2023年4月19日 17:52:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053114.html
匿名

发表评论

匿名网友

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

确定