time.sleep(5)
driver.implicitly_wait(30)
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get('https://huilansame.github.io')
locator = (By.LINK_TEXT, 'CSDN')
try:
WebDriverWait(driver, 20, 0.5).until(EC.presence_of_element_located(locator))
print driver.find_element_by_link_text('CSDN').get_attribute('href')
finally:
driver.close()
WebDriverWait(driver, 超时时长, 调用频率, 忽略异常).until(可执行方法, 超时时返回的信息)
| 函数 | 说明 |
|---|---|
| presenceOfElementLocated(locator) | 是否存在 |
| visibilityOfElementLocated(locator) | 是否可见 |
| invisibilityOfElementLocated(locator) | 是否不可见 |
| elementToBeClickable(locator) | 是否可见并且enable |
| elementToBeSelected(locator) | 是否被选中了,一般用在下拉列表 |
| alertIsPresent() | 判断页面上是否存在alert |
| textToBePresentInElement(locator,text) | text是否包含字符串 |
| textToBePresentInElementValue(locator,text) | value是否包含字符串 |
| titleIs(title) | title是否等于预期 |
| titleContains(title) | title是否包含字符串 |
web.set_page_load_timeout(5)
try:
web.get(url)
except:
print('timeout')
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
desired_capabilities = DesiredCapabilities.CHROME
desired_capabilities["pageLoadStrategy"] = "none"
本文章使用limfx的vsocde插件快速发布