我正在使用selenium根据页面标题将媒体重命名并排序到文件夹中,但页面仍在后台加载内容,并且在firefox完成下载和显示内容后页面标题发生变化。
每当我点击系列纪录片中不是该系列第一集的一集时,就会发生此问题。它返回剧集名称而不是系列标题,但在后台内容完成加载浏览器标题和 html 标签文本后,我需要更改系列标题,这就是我想要的。
我已经在 google 上搜索,并在 stackoverflow.com 上搜索了好几天。我已经浏览了selenium模块的几乎每个部分,尝试了不同的东西,而且我已经浏览了网页的每个部分,希望找到一些可以用来让selenium等待内容完成加载的东西,但没有运气.
此外,另一个问题的答案中建议将
WebDriverWait
与 expected_conditions
一起使用,并尽量避免 time.sleep
与 selenium
一起使用,我明白了。即使在高速互联网连接上,也有很多因素会减慢网页的加载时间,从而导致 time.sleep
不一致。
我从标题本身开始使用...
import selenium.webdriver.support.expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait
browser = Firefox()
wait = WebDriverWait(driver = browser, timeout = 30)
browser.get('https://curiositystream.com/video/3558')
wait.until_not(ec.title_is(current_title))
但是,目前我已经解决了这个问题。我遇到的问题并不经常发生,但问题仍然存在。
print(wait.until(
ec.visibility_of_element_located((
'xpath',
'//button[@aria-expanded="false" and @class="inline-block cursor-pointer"]'
'/span[@class="leading-tight text-lg tablet:text-2xl font-normal" and contains(@aria-label,"Show")]'
))).text, end = ' ')
if len(browser.find_elements(
by = 'xpath',
value = '//div[@class="pt-4"]/p[@class="font-medium text-light pt-2"]'
)) > 0:
print('(Docuseries)')
else:
print('(Documentary)')
这不是我所写内容的实际来源,但它可以重现我遇到的问题。我希望有人愿意通过硒和好奇心流进行探索,帮助我想出一个无需使用
time.sleep
即可工作的解决方案。
from contextlib import suppress
from os import getpid, kill
from re import compile
from signal import SIGTERM
from time import sleep # noqa
import selenium.common.exceptions as exc
import selenium.webdriver.support.expected_conditions as ec
from selenium.webdriver import Firefox
from selenium.webdriver.support.wait import WebDriverWait
# from library import Firefox
if __name__ == '__main__':
browser = Firefox()
wait = WebDriverWait(driver = browser, timeout = 30)
browser.set_window_rect(x = 960, y = 10, width = 1920, height = 1580)
browser.get('https://curiositystream.com/')
url = compile(r'https://curiositystream.com/video/[0-9]+')
# current_url = current_title = ''
current_url, current_title = browser.current_url, browser.title
try:
while True:
if current_url != browser.current_url:
current_url = browser.current_url
wait.until_not(ec.title_is(current_title))
if url.match(string = browser.current_url):
current_title = browser.title
if (button := next((_ for _ in browser.find_elements(
by = 'xpath',
value = '//button[@class="vjs-big-play-button" and '
'@type="button" and '
'@title="Play Video" and '
'@aria-disabled="false"]'
)), None)) is not None:
with suppress(
exc.StaleElementReferenceException,
exc.ElementNotInteractableException
):
button.click()
# sleep(wait.__dict__['_poll'])
print(wait.until(
ec.visibility_of_element_located((
'xpath',
'//button[@aria-expanded="false" and @class="inline-block cursor-pointer"]'
'/span[@class="leading-tight text-lg tablet:text-2xl font-normal" and contains(@aria-label,"Show")]'
))).text, end = ' ')
if len(browser.find_elements(
by = 'xpath',
value = '//div[@class="pt-4"]/p[@class="font-medium text-light pt-2"]'
)):
print('(Docuseries)')
else:
print('(Documentary)')
except exc.NoSuchWindowException:
kill(getpid(), SIGTERM)