我编写了一个从页面获取数据的脚本,但有时页面需要时间来加载,所以当它将html拉入汤对象时,它会拉动任何内容,因为页面仍然需要完成。
我编写了以下代码来等待页面完成。
results = [pool.map(plot_var, a) for a in agrs]
它有效
但是当我调用该函数时,我收到以下错误;
def scrape_page(url):
browser.get(url)
try:
WebDriverWait(browser, 10).until(EC.presence_of_element_located(browser.find_element_by_id ("selection-box")))
#Extract Source Code
html = browser.page_source;
soup = BeautifulSoup(html)
答案 0 :(得分:8)
我认为你应该像这样使用presence_of_element_located
:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
如manual。
中所述答案 1 :(得分:1)
我将此函数应用于我需要使用的每个 WebElement。
from selenium import webdriver
def FindElem(Driver: webdriver, XPath: str, Timeout: int = 300):
while Timeout > 0:
try:
return Driver.find_element_by_xpath(XPath)
except: # if element isn't already loaded or doesn't exist
time.sleep(1)
Timeout -= 1
raise RuntimeError(f"Page loading timeout") # or whatever the hell you want
用法:
Driver = webdriver.Firefox()
webdriver.get("http://somewhere.com/somepage.html")
MyWebElement = FindElem(Driver, "//input[@name='email']") # raise exception if timeout