英文:
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:
<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.
答案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="Button__StyledButton-a1qza5-0 lcqSKB"]
or
/*/button[contains(@class, 'Button__StyledButton-a1qza5-0')]
results in:
driver.find_element_by_xpath('/*/button[@class="Button__StyledButton-a1qza5-0 lcqSKB"]').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("Button__StyledButton-a1qza5-0.lcqSKB")
driver.find_element_by_css_selector("Button__StyledButton-a1qza5-0")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论