使用 Selenium WebDriverWait 选择带有文本的按钮标签时出现问题

Issue selecting button tag with text using Selenium WebDriverWait

提问人:Claytor 提问时间:10/5/2023 最后编辑:Claytor 更新时间:10/5/2023 访问量:29

问:

我无法选择具有特定文本的按钮(因此无法单击它)。

这是按钮的图像:

enter image description here

这是它的 HTML 代码:

enter image description here

使用 WebDriverWait 和文本“确认付款”时,该按钮会正确选择。

WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='Button_content__guezZ' and text()='Confirm payment:']")))

我需要的是根据金额选择按钮,这意味着在文本中包含价格,但是当我这样做时,WebDriverWait将不再起作用。

我尝试了几种方法,基本上使用上面相同的 WebDriverWait,在文本中添加一些变体:、、。他们都没有运气。'Confirm payment: 0,01 €''0,01 €''0,01 €'

当仅使用“确认付款:”时,代码有效,另一方面,当我包含金额时,它不再有效。

我的想法快用完了。我做错了什么?

python selenium-webdriver webdriverwait

评论


答:

0赞 Tal Angel 10/5/2023 #1

您需要过滤按钮文本。使用 Xpath 文本包含 - 首先是“付款”一词,然后是您需要支付的金额。

xpath_exp = '//div[@class="Button_content__guezZ"]
[contains(text(), "payment")][contains(text(), "0,01")]'
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, xpath_exp)))

您可以使用此方法连接属性筛选,并查找包含 2 个或更多文本短语的特定元素。