Python Selenium:如何单击包含文本的元素以及按钮的父元素

Python Selenium: How to click an element containing text and which parent is button

我在包含“Einloggen”文本的页面上有两个按钮,我需要单击“Einloggen”按钮。

第一个元素是“Probleme beim Einloggen?”是具有跨度的 link。

第二个,我需要的是一个带有 span 的按钮。

CSS 类不稳定,不能用

我试过:

self.log_in_link = WebDriverWait(self.driver, self.delay).until(
            EC.presence_of_element_located((By.XPATH, "//button/following-sibling::span[contains(text(), 'Einloggen')]")))

但是找不到

如果我试试

self.log_in_link = WebDriverWait(self.driver, self.delay).until(
            EC.presence_of_element_located((By.XPATH, ".//span[contains(text(), 'Einloggen')]")))

驱动程序点击了错误的元素(“Probleme beim Einloggen?”)

从你提供的图片我看到这里的父元素不是 button 而是 divbuttonstyle class.
请试试这个 XPath

//div[contains(@class,'buttonstyle')]//span[contains(text(),'Einloggen')]

我还建议使用 visibility_of_element_located 预期条件而不是 presence_of_element_located 来等待更成熟的元素状态,当它变得可见时,而不仅仅是呈现(但可能仍未完全呈现)。
所以你的代码可以是

WebDriverWait(self.driver, self.delay).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class,'buttonstyle')]//span[contains(text(),'Einloggen')]")))