当文本更改为其他内容时,如何使用Selenium和Python单击按钮?

当文本更改为其他内容时,如何使用Selenium和Python单击按钮?,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我一直在测试一些新的浏览器自动化工具。其中之一是硒。我正在做的一件事是使用Python打开网页。转到该网页并查找某个按钮。如果按钮显示“Yes button”,则不执行任何操作,只刷新页面。如果刷新后按钮更改为“无按钮”,则单击该按钮。我将在下面发布代码。我只漏掉了我的网址。非常感谢您的帮助。目前,我下面的脚本执行刷新部分,但当按钮更改为“No button”时,它将停止并不会单击按钮。我不确定我的while循环是错误的还是我对Selenium的理解是错误的 from selenium impo

我一直在测试一些新的浏览器自动化工具。其中之一是硒。我正在做的一件事是使用Python打开网页。转到该网页并查找某个按钮。如果按钮显示“Yes button”,则不执行任何操作,只刷新页面。如果刷新后按钮更改为“无按钮”,则单击该按钮。我将在下面发布代码。我只漏掉了我的网址。非常感谢您的帮助。目前,我下面的脚本执行刷新部分,但当按钮更改为“No button”时,它将停止并不会单击按钮。我不确定我的while循环是错误的还是我对Selenium的理解是错误的

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
import time
################import the chrome web driver and define the location###############
PATH = "c:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
###################################################################################

###########open the web page and print the title##############
driver.get("https://mywebsite.com")
print(driver.title)
time.sleep(1)
##############################################################

#Look for search button and wait for it to change to something else. 
while True:
    searchbutton = WebDriverWait(driver, 60).until(expected_conditions.presence_of_element_located((By.LINK_TEXT, "Yes Button")))
    driver.refresh()
else:
    searchbutton = driver.find_element_by_link_text("No Button")
    searchbutton.click()
程序永远不会到达该部分,因为当为True时,它永远不会退出
。应该是这样的。此外,意外情况将使驱动程序等待元素出现故障,这将导致超时错误。所以,我们就这样做

   while True:
    button1 = driver.find_element_by_xpath('//*[@id="comp-khm867e6"]/a/span')
   
    if 'Yes' in button1.text:
        driver.refresh()
        time.sleep(10)
    elif 'No' in button1.text:
        button1.click()
        break

给出实际的链接这只是我建立的一个小测试网站。我把一个按钮放在那里,就是这样@jkotts什么时候变为“否”按钮?我刷新了按钮。它保持不变?我必须手动更改它。如果您愿意,我可以为您更改它。因此,您是说,当您将按钮的文本标签从“是”按钮更改为“否”按钮时,它找不到它?注释不用于扩展讨论;这段对话已经结束。
   while True:
    button1 = driver.find_element_by_xpath('//*[@id="comp-khm867e6"]/a/span')
   
    if 'Yes' in button1.text:
        driver.refresh()
        time.sleep(10)
    elif 'No' in button1.text:
        button1.click()
        break