python 如何用 selenium 蟒监测元素的变化

41zrol4v  于 2022-10-30  发布在  Python
关注(0)|答案(1)|浏览(168)

我想做一个监视器来检查特定元素文本中的变化,我不知道我该怎么做。我知道如何检查元素中当前的内容,但我不知道如何将其与新的更改进行比较并给予我更新。有人知道我该怎么做吗?这是我编写的当前代码,用于查找元素的文本并将其存储在一个变量中,但我不确定下一步该做什么

driver = webdriver.Chrome(
        executable_path=r"ChromePath, options=option)
 driver.get("Mywebsite")
 sleep(3)
 link = driver.find_element("xpath", 'xpath goes here')
 link.click()
 sleep(2)
 text = driver.find_element("id", 'name')
 text.send_keys("what im searching for")
 sleep(2)
 button = driver.find_element('id', "name")
 button.click()
 sleep(2)
 element = driver.find_element("xpath", 'xpath goes here')
 currentText = element.text #current text of element
7rtdyuoh

7rtdyuoh1#

只需添加一个while循环,该循环将反复检查此元素,直到它被更改

currentText = element.text #current text of element
updatedText = element.text
while currentText == updatedText:
    updatedText = driver.find_element("xpath", 'xpath goes here').text
    # You can put here any sleep to repeat this check once in some time. For example once in 10 seconds
    sleep(10)

# Now you can do anything with the new text, for example print it

print(updatedText)

相关问题