Selenium 在搜索元素时等待很多

Selenium wait a lot while searching for element

我正在开发一种在网站源代码中查找电子邮件地址的软件。 但是有时候网站的源码很长,所以要找很久。 如何为此设置特定时间并在该时间到期后切换到其他网站url?

    for query in my_list:
        results.append(search(query, tld="com", num=3, stop=3, pause=2))

    for result in results:
        url = list(result)
        print(*url,sep='\n')
        for site in url:
            driver = webdriver.Chrome()
            driver.get(site)
            doc = driver.page_source
            emails = re.findall(r'[\w\.-]+@[\w\.-]+', doc)
            for email in emails:
                print(email)

结果=[] start_time = time.time() my_list 中的查询: results.append(搜索(查询, tld="com", num=3, 停止=3, 暂停=2))

for result in results:
    url = list(result)
    print(*url,sep='\n')
    for site in url:
        driver = webdriver.Chrome()
        driver.get(site)
        doc = driver.page_source
        emails = re.findall(r'[\w\.-]+@[\w\.-]+', doc)
        for email in emails:
            print(email)
if time.time() - start_time > 10:
    # if 10 seconds pass do something
    start_time = time.time()
time.sleep(3)
driver.close()

您可以稍等片刻,使用 python 时间模块提供时间,如下所示:

import time

for site in url:
    driver = webdriver.Chrome()
    driver.get(site)
    time.sleep(8)

       
import time
start_time = time.time()

# your code

while True:
    if time.time() - start_time > 2:
        # if 2 seconds pass do something
        start_time = time.time()
        print("2 seconds passed")