标签: webdriverwait

如何使用 Selenium WebDriver 等待重定向链在最终页面加载不可预测的情况下解决?

我想自动化一个脚本来验证网页中的某些内容。

登录页面在登陆应用程序页面之前重定向了几次,这些重定向偶尔会导致问题。这个问题肯定与时间有关。那么 webdriver 如何等到最终页面完全加载或稳定?

典型的解决方案是等待目标页面上的元素变为可用。不幸的是,我们使用了 100 多个唯一网址,因此没有可预测的着陆页。

那么如何实现一个 ExpectedCondition 来等待应用程序在重定向方面稳定下来呢?

主要议程是,我有一组 URL,我想通过检查其中的内容来验证所有这些 URL 是否有效。所有这些都是我的组织合作的学习内容的直接 URL,例如 Lynda、skillsoft 等。因此,当我们首先通过自动脚本启动这些 URL 时,它会询问身份验证(我的组织凭据),然后它会重定向到相应的站点。在这些重定向期间,在某些情况下会发生多次重定向,最后加载内容页面。这就是场景。我猜我的脚本失败主要是因为等待

url selenium redirect selenium-webdriver webdriverwait

6
推荐指数
1
解决办法
6230
查看次数

如何在 Python 中使 Selenium WebDriver 休眠几毫秒

time在我的脚本中使用库:

import time
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)

它可以让我的 Selenium WebDriver 休眠一秒钟,但 250 毫秒怎么可能呢?

python selenium webdriver selenium-webdriver webdriverwait

6
推荐指数
1
解决办法
2万
查看次数

Python,Selenium 查找带有类的元素并等待类更改

我有一个动态加载内容的网页,当页面加载时,有一个旋转轮,我已经找到了抓取页面上立即加载的内容的解决方案,但似乎我找不到抓取稍后在 dom 中加载的内容的解决方案。

我能想到的是找到具有该轮子旋转的特定类的元素,并等待它更改,一旦更改,这意味着内容已加载到 dom 中。

我使用SeleniumFirefox webdriverUbuntu

这是我要监控的课程:

<div class="wheel spinning"></div>
Run Code Online (Sandbox Code Playgroud)

加载内容后,轮子停止旋转并且类更改为:

<div class="wheel"></div>
Run Code Online (Sandbox Code Playgroud)

任何人都可以找到解决方案来查找和监控class="wheel spinning",一旦更改class="wheel"为继续抓取数据。

编辑:

XPATH实际上解决了一部分解决方案,这里是部分代码

try:
    element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']))
)
title = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[3]')
print(title.text)
Run Code Online (Sandbox Code Playgroud)

但是如果元素在 10 秒内没有出现,那就是错误了,现在想办法一次又一次地重试,直到元素出现在页面上。

使用presence_of_element_located((By.XPATH))和使用上有区别吗find_element_by_xpath

python selenium css-selectors selenium-webdriver webdriverwait

6
推荐指数
2
解决办法
9978
查看次数

Selenium Python 不关闭子窗口

我有一个网页,点击后会打开新的浏览器窗口。我可以获得 2 个句柄,但是 driver.close() 总是关闭第一个/主窗口。

from selenium import webdriver
import time
driver = webdriver.Chrome() 
driver.get("file:///D:/blackhole/print.html")
han = driver.window_handles
print("handles:", han) # gets 1 handle
time.sleep(2)
click_btn = driver.find_element_by_link_text('Print')
click_btn.click()
han = driver.window_handles
print("handles:", han) # gets 2 handles
driver.switch_to_window = han[1] # first element is always first window handle
driver.close() # main window close
Run Code Online (Sandbox Code Playgroud)

下面的网页代码调用新窗口

<a href="print.html"  
onclick="window.open('popprint.html', 
                    'newwindow', 
                    'width=300,height=250'); 
        return false;"
>Print</a>
Run Code Online (Sandbox Code Playgroud)

Firefox 也有同样的行为。Python 3.6.7

selenium window-handles python-3.x selenium-webdriver webdriverwait

6
推荐指数
1
解决办法
1万
查看次数

Selenium python错误:元素无法滚动到视图中

我正在为我的公司自动化IdentiGO应用程序,并且出现以下错误:

Internal Server Error: /identigo
Traceback (most recent call last):
  File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/views/generic/base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/jane/Code/maynard_env/maynard/employee/views.py", line 63, in post
    driver.main(employee)
  File "/Users/jane/Code/maynard_env/maynard/employee/driver.py", line 31, in main
    WebDriverWait(driver, 1000000).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[5]/div[3]/div/button/span'))).click()
  File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 80, …
Run Code Online (Sandbox Code Playgroud)

python firefox selenium geckodriver webdriverwait

6
推荐指数
2
解决办法
1730
查看次数

你如何使用 EC.presence_of_element_located((By.ID, "myDynamicElement")) 除了指定类而不是 ID

我正在尝试使用 Python 来抓取一个网站,该网站通过使用嵌入的 javascript 文件将数据作为响应呈现到 HTML 中来动态加载它的 HTML。因此,如果我单独使用 BeautifulSoup,我将无法检索我需要的数据,因为我的程序会在 Javascript 加载数据之前抓取它。因此,我将 selenium 库集成到我的代码中,以使我的程序在抓取网站之前等待找到某个元素。

我最初是这样做的:

element = WebDriverWait(driver,100).until(EC.presence_of_element_located((By.ID, "tabla_evolucion")))
Run Code Online (Sandbox Code Playgroud)

但我想通过执行以下操作来指定一个类:

element = WebDriverWait(driver,100).until(EC.presence_of_element_located((By.class, "ng-binding ng-scope")))  
Run Code Online (Sandbox Code Playgroud)

这是我的其余代码:

driver_path = 'C:/webDrivers/chromedriver.exe'
driver = webdriver.Chrome(executable_path=driver_path)
driver.header_overrides = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'}
url = "myurlthatIamscraping.com" 
response = driver.get(url)
html = driver.page_source
characters = len(html)
element = WebDriverWait(driver,100).until(EC.presence_of_element_located((By.class, "ng-binding ng-scope")))

print(html)
print(characters)
time.sleep(10)
driver.quit()
Run Code Online (Sandbox Code Playgroud)

它对我不起作用,我在任何地方都找不到正确的语法。

python selenium selenium-webdriver webdriverwait expected-condition

6
推荐指数
2
解决办法
3万
查看次数

如何使用 Selenium 和 Python 调用函数之前等待特定 URL 加载

这是我第一次在 stackoverflow 上发帖,我对 Selenium 和 Python 还有些陌生。

当 URL 等于 fx: https://www.example.com时,我不希望运行函数。

我在另一个讨论中读过 这个答案,但我不太明白发生了什么。

我希望您能抽出时间回答我的问题。

好的,所以我刚刚尝试过这个:

driver.get('https://www.google.com')
time.sleep(4)
driver.get('https://www.stackoverflow.com')

if WebDriverWait(driver, 10).until(EC.url_to_be('https://stackoverflow.com')):
    print('Desired url was rendered within allocated time')
else:
    print('Desired url was not rendered within allocated time')
Run Code Online (Sandbox Code Playgroud)

但这没有用。有任何想法吗?
控制台说

Traceback (most recent call last):
  File "/Users/holger/PycharmProjects/waitTest/wait.py", line 15, in <module>
    if WebDriverWait(browser, 10).until(EC.url_to_be('https://www.stackoverflow.com')):
  File "/Users/holger/PycharmProjects/waitTest/venv/lib/python3.8/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Run Code Online (Sandbox Code Playgroud)

python url selenium webdriver webdriverwait

6
推荐指数
1
解决办法
6700
查看次数

通过 Selenium 和 Python 使用带有预期条件的 WebDriverWait 时出现 selenium.common.exceptions.TimeoutException 错误

Traceback (most recent call last):
  File "Inventorytest.py", line 88, in <module>
    j.go_to_application()
  File "Inventorytest.py", line 65, in go_to_application
    EC.element_to_be_clickable((By.ID, 'FavoriteApp_ITEM'))
  File "/home/naroladev/Mercury_Back-End/mercuryenv/lib/python3.6/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Run Code Online (Sandbox Code Playgroud)

我在 EC2 服务器实例上遇到了上述异常。我的脚本在 Ubuntu 和 Mac 操作系统以及本地系统上任何版本的 firefox 和 geckodriver 上都可以正常工作。但是 EC2 ubuntu 18.04.01 版本出现上述错误,在此我也尝试升级和降级 firefox 和 geckodriver 版本,但仍然无法正常工作。谁能帮我提供建议和解决方案。

python selenium timeoutexception webdriverwait expected-condition

6
推荐指数
1
解决办法
2万
查看次数

Python/Selenium 网络废料如何从链接中查找隐藏的 src 值?

抓取链接应该是一件简单的事情,通常只需获取srca 标签的值即可。

我最近发现这个网站(https://sunteccity.com.sg/promotions),其中每个项目的a标签的href值都找不到,但重定向仍然有效。我正在尝试找出一种方法来获取项目及其相应的链接。我典型的 python selenium 代码看起来像这样

all_items = bot.find_elements_by_class_name('thumb-img')
for promo in all_items:
    a = promo.find_elements_by_tag_name("a")
    print("a[0]: ", a[0].get_attribute("href"))
Run Code Online (Sandbox Code Playgroud)

但是,我似乎无法检索任何href,onclick属性,我想知道这是否可能。我注意到我也无法在新选项卡中右键单击打开链接。

有什么方法可以获取所有这些项目的链接吗?

编辑: 有什么方法可以检索页面上项目的所有链接吗?

IE

https://sunteccity.com.sg/promotions/724
https://sunteccity.com.sg/promotions/731
https://sunteccity.com.sg/promotions/751
https://sunteccity.com.sg/promotions/752
https://sunteccity.com.sg/promotions/754
https://sunteccity.com.sg/promotions/280
...
Run Code Online (Sandbox Code Playgroud)

编辑:添加这样一个锚标记的图像以获得更好的清晰度: 在此输入图像描述

python selenium xpath css-selectors webdriverwait

6
推荐指数
1
解决办法
727
查看次数

消息:尝试通过 Selenium 单击下拉菜单中的选项时,元素 &lt;option&gt; 无法滚动到视图中

我正在尝试选择一个下拉菜单并选择一个选项。我正在使用最新版本的 Selenium、最新版本的 Firefox、最新版本的 geckodriver 和最新版本的 Python。

这是我的问题:当我尝试选择一个选项时,出现以下错误:

selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view.
Run Code Online (Sandbox Code Playgroud)

我尝试了各种方法来解决这个问题,但似乎都不起作用。以下是我尝试过的一些方法。

mySelectElement = browser.find_element_by_id('providerTypeDropDown')
dropDownMenu = Select(mySelectElement)
dropDownMenu.select_by_visible_text('Professional')

mySelectElement = browser.find_element_by_id('providerTypeDropDown')
dropDown = Select(mySelectElement)
for option in dropDown.options:
    message = option.get_attribute('innerText')
    print(message)
    if message == 'Professional':
        print("Exists")
        dropDown.select_by_visible_text(message) 
        break

element = browser.find_element_by_id('providerTypeDropDown')
browser.execute_script("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", element, "Professional")
Run Code Online (Sandbox Code Playgroud)

HTML 代码遵循通常的选择标签和选项标签。任何帮助表示赞赏。HTML 代码包含在下面。

<select data-av-chosen="providerTypes" id="providerTypeDropDown" data-placeholder="Please Select a …
Run Code Online (Sandbox Code Playgroud)

python selenium selenium-webdriver drop-down-menu webdriverwait

5
推荐指数
1
解决办法
3万
查看次数