[嗨,我正在尝试使用硒来刮擦网站,但该网站有时会阻止我的IP进行刮擦,并会出现一个弹出消息。
我想做的是,每当出现该弹出消息时,它都会在我的终端上显示警告,我的代码看起来像这样以得到结果
blo = driver.find_element_by_xpath('/html/body/div[2]/div/div/div[1]/h3')
if blo:
print('ip blocked')
else:
print('eroor')
但是它不起作用,我只是得到一个空白屏幕,我该如何解决这个问题
元素的HTML:
<h3 class="modal-title">Notifications</h3>
<div class="alert alert-warning">Sorry, you have exceeded the maximum number of queries allowed per day. If you believe you have reached this message in error, please contact our support team.</div>
验证弹出消息]的存在,您必须为visibility_of_element_located()
引入WebDriverWait,并且可以使用以下任何一个Locator Strategies:
使用XPATH
:
try:
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h3[@class='modal-title' and text()='Notifications']//following::div[@class='alert alert-warning']")))
print("ip blocked")
except TimeoutException:
print("eroor")