我尝试使用 python 单击反应页面中的按钮,但每次重新加载页面时它都会像令牌一样生成,我不知道如何让它发挥作用 这是按钮:
<button class="mantine-UnstyledButton-root mantine-Button-root mantine-ap0q7a" type="submit" data-button="true">
<div class="mantine-1wpc1xj mantine-Button-inner">
<span class="mantine-1ryt1ht mantine-Button-label">Click to login</span></div></button>
编辑:那个 “mantine-1ryt1ht” 部分是把它弄乱的部分。 Mantine 始终是相同的,但以下部分是每次页面加载时都会更改的部分。
我尝试使用这个,但它不起作用:
for button in WebDriverWait(chrome, 1200).until(EC.visibility_of_all_elements_located((By.XPATH, "//button[text()='Click to login']"))):
button.click()
这是我完成项目时唯一缺少的东西,我不想使用像 pyautogui 这样的东西,因为我希望它最小化运行。
请将我从缺乏知识的循环中拯救出来。
所以这是一个按钮元素,它将在每个页面上都是常量,因此如果我们使用这样的循环:
def find&click():
buttons = driver.find_elements(By.TAG_NAME, 'button') #this will get all the buttons on the page
for button in buttons: #this will iterate through all the buttons
if button.text == "Click to login" #this will find the one called "Click to login"
button.click() #this clicks the button
find&click()
所以这个程序会查找按钮的所有 html 标签,即 一旦找到,它将遍历每一个,查看每一个的文本,直到找到带有“点击登录”文本的一个。 一旦找到,它将单击按钮。 这不会依赖任何类名、ids 或 xpath,并且永远不会改变。
现在应该始终可以找到页面上的按钮并单击它。如果您需要任何其他愉快的编码,请告诉我:)
button[text()
将不起作用,因为该字符串没有直接的 text() 节点。
您可以尝试以下 XPath:
A.使用 text()-node 的显式路径
//button[div/span/text()='Click to login']
或
B.使用
normalize-space(.)
,其中 .
是组合的 text() 节点,标准化空间将消除可选的空白字符。
//button[normalize-space(.)='Click to login']
或
C.在组合的 text() 节点上使用 contains。
//button[contains(.,'Click to login')]