如何使用 chromedriver Selenium 和 Python 从临时警报中获取文本?
How can I get the text from an temporary alert using chromedriver Selenium and Python?
我正在编写一个程序,使用 chromedriver 自动在我的图书馆预订书桌。我正在遍历所有座位,寻找尚未预订的座位,然后使用
driver.find_element(By.XPATH, xpath).click()
关于预订座位的开始和结束时间。
单击后,可能会弹出不同的警报,确认预订或指出问题。这些警报会在几秒钟后消失。我正在尝试使用弹出窗口的文本来验证预订是否成功。
到目前为止,我试过这个:
try:
WebDriverWait(driver, 10).until(EC.alert_is_present)
alert = driver.switch_to.alert
text = alert.text
if "some Text" in text:
print("Booking successful")
elif "some other text" in text:
print("booking not successful")
except:
print("no alert")
但是,警报没有被识别,我总是得到“没有警报”。
警报是这样的:
<div data-notify="container" class="col-xs-11 col-sm-4 alert alert-success animated fadeInDown" role="alert" data-notify-position="top-center" style="display: inline-block; margin: 0px auto; position: fixed; transition: all 0.5s ease-in-out 0s; z-index: 5000; top: 20px; left: 0px; right: 0px;">
<button type="button" aria-hidden="true" class="close" data-notify="dismiss" style="position: absolute; right: 10px; top: 5px; z-index: 5002;"> × </button>
<span data-notify="icon"> </span>
<span data-notify="title"></span>
<span data-notify="message"> Booking successful. </span>
<a href="#" target="_blank" data-notify="url"> </a>
</div>
looks like an due to the presence of classnames like alert
, alert-success
, etc. But it's not an actual
要打印文本 预订成功。 来自 警报 您可以使用以下任一方法 :
使用css_selector和get_attribute("innerHTML")
:
print(driver.find_element(By.CSS_SELECTOR, "div[class*='alert'][data-notify='container'] span[data-notify='message']").get_attribute("innerHTML"))
使用 xpath 和 text 属性:
print(driver.find_element(By.XPATH, "//div[contains(@class, 'alert') and @data-notify='container']//span[@data-notify='message']").text)
理想情况下你需要诱导 for the and you can use either of the following :
使用 CSS_SELECTOR 和 text 属性:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[class*='alert'][data-notify='container'] span[data-notify='message']"))).text)
使用 XPATH 和 get_attribute("innerHTML")
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class, 'alert') and @data-notify='container']//span[@data-notify='message']"))).get_attribute("innerHTML"))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
You can find a relevant discussion in
参考资料
Link 到有用的文档:
get_attribute()
方法Gets the given attribute or property of the element.
text
属性returnsThe text of the element.
- Difference between text and innerHTML using Selenium
我正在编写一个程序,使用 chromedriver 自动在我的图书馆预订书桌。我正在遍历所有座位,寻找尚未预订的座位,然后使用
driver.find_element(By.XPATH, xpath).click()
关于预订座位的开始和结束时间。 单击后,可能会弹出不同的警报,确认预订或指出问题。这些警报会在几秒钟后消失。我正在尝试使用弹出窗口的文本来验证预订是否成功。
到目前为止,我试过这个:
try:
WebDriverWait(driver, 10).until(EC.alert_is_present)
alert = driver.switch_to.alert
text = alert.text
if "some Text" in text:
print("Booking successful")
elif "some other text" in text:
print("booking not successful")
except:
print("no alert")
但是,警报没有被识别,我总是得到“没有警报”。
警报是这样的:
<div data-notify="container" class="col-xs-11 col-sm-4 alert alert-success animated fadeInDown" role="alert" data-notify-position="top-center" style="display: inline-block; margin: 0px auto; position: fixed; transition: all 0.5s ease-in-out 0s; z-index: 5000; top: 20px; left: 0px; right: 0px;">
<button type="button" aria-hidden="true" class="close" data-notify="dismiss" style="position: absolute; right: 10px; top: 5px; z-index: 5002;"> × </button>
<span data-notify="icon"> </span>
<span data-notify="title"></span>
<span data-notify="message"> Booking successful. </span>
<a href="#" target="_blank" data-notify="url"> </a>
</div>
alert
, alert-success
, etc. But it's not an actual
要打印文本 预订成功。 来自 警报 您可以使用以下任一方法
使用css_selector和
get_attribute("innerHTML")
:print(driver.find_element(By.CSS_SELECTOR, "div[class*='alert'][data-notify='container'] span[data-notify='message']").get_attribute("innerHTML"))
使用 xpath 和 text 属性:
print(driver.find_element(By.XPATH, "//div[contains(@class, 'alert') and @data-notify='container']//span[@data-notify='message']").text)
理想情况下你需要诱导
使用 CSS_SELECTOR 和 text 属性:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[class*='alert'][data-notify='container'] span[data-notify='message']"))).text)
使用 XPATH 和
get_attribute("innerHTML")
:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class, 'alert') and @data-notify='container']//span[@data-notify='message']"))).get_attribute("innerHTML"))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
You can find a relevant discussion in
参考资料
Link 到有用的文档:
get_attribute()
方法Gets the given attribute or property of the element.
text
属性returnsThe text of the element.
- Difference between text and innerHTML using Selenium