Selenium是啥

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Selenium是啥相关的知识,希望对你有一定的参考价值。

参考技术A Selenium是一个基于浏览器的自动化工具,它提供了一种跨平台、跨浏览器的端到端的web自动化解决方案。Selenium主要包括三部分:

· Selenium IDE: Firefox浏览器的一个插件(扩展),它可以进行录制回放,并且可以把录制的操作以多种语言(如JAVA、Python、C#等)的形式导出成测试用例。

· Selenium WebDriver: 提供Web自动化所需的API,主要用作浏览器控制、页面元素选择和调试。不同的浏览器需要不同的WebDriver。

· Selenium Grid: 提供了在不同机器的不同浏览器上运行selenium测试的能力。

这里的框架主要使用Python结合Selenium WebDriver库进行搭建的。通常,一个典型的自动化测试框架一般包括用例管理模板、自动化执行控制器、报表生成模块、日志模块和邮件发送模块等。

设置 Headless Selenium 的最新方法是啥

【中文标题】设置 Headless Selenium 的最新方法是啥【英文标题】:What is the latest way to set up Headless Selenium设置 Headless Selenium 的最新方法是什么 【发布时间】:2022-01-16 05:41:33 【问题描述】:

抱歉,我对 Python 很陌生。 我在网上查了一下,发现有很多视频都有创建无头谷歌硒的方法。但是,在尝试这些方法后,我意识到它们都已被弃用。谁能用最新的方法更新我为谷歌 Chrome 启用无头 Selenium 的方法?

所以程序的顺序如下:

    进入登录页面网站“https://info.bbdc.sg/members-login/” 然后,它会提交不安全的页面“http://www.bbdc.sg/bbdc/bbdc_web/header2.asp” photo 最后,它会导航到网站的主页,即“http://www.bbdc.sg/bbdc/b-mainframe.asp”

[更新] 我尝试将建议的答案添加到我的代码中。 这是我的新代码:

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options

browser_options = Options()
browser_options.add_argument("--allow-running-insecure-content")
browser_options.headless = True
browser = webdriver.Chrome(executable_path=r'C:\Users\Jonathan\Projects\drivers\chromedriver_win32_V96\chromedriver.exe',options=browser_options)
browser.get("https://info.bbdc.sg/members-login/")

login_refresh = True
while login_refresh:
    try:
        if EC.url_matches('https://info.bbdc.sg/members-login/?err=Please+refresh+your+browser+and+fill+in+the+fields'
                          '+properly+to+login.+' or 'https://info.bbdc.sg/members-login/'):
            browser.refresh()
            login_id_elem = browser.find_element(By.ID, 'txtNRIC')
            login_pw_elem = browser.find_element(By.ID, 'txtPassword')
            access_btn = browser.find_element(By.ID, 'loginbtn')
            login_id_elem.send_keys("094H28022001")
            login_pw_elem.send_keys("258046")
            time.sleep(5)
            access_btn.click()
    except NoSuchElementException:
        login_refresh = False

print(browser.current_url)
WebDriverWait(browser, 10).until(EC.url_matches('http://www.bbdc.sg/bbdc/bbdc_web/header2.asp'))
send_anyway = browser.find_element(By.ID, 'proceed-button')
print(browser.current_url)
send_anyway.click()

# Home page
print(browser.current_url)
frame_element = browser.find_element(By.XPATH, '//frame[@name="leftFrame"]')
browser.switch_to.frame(frame_element)

但是,我在运行 headless chrome 时收到此堆栈跟踪错误。如果我要使用普通的 headful chrome,它可以正常工作。

DevTools listening on ws://127.0.0.1:57679/devtools/browser/34af1be6-ebe7-4326-a4b5-072d940d9684
[1213/220648.128:INFO:CONSOLE(23)] "JQMIGRATE: Migrate is installed with logging active, version 1.4.1", source: https://info.bbdc.sg/
wp-content/plugins/enable-jquery-migrate-helper/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (23)
[1213/220648.168:INFO:CONSOLE(56)] "JQMIGRATE: jQuery.fn.load() is deprecated", source: https://info.bbdc.sg/wp-content/plugins/enable
-jquery-migrate-helper/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (56)
[1213/220648.168:INFO:CONSOLE(58)] "console.trace", source: https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-helper/js/jq
uery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (58)
[1213/220648.315:INFO:CONSOLE(825)] "Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')", source: https:/
/info.bbdc.sg/members-login/ (825)
[1213/220648.334:INFO:CONSOLE(56)] "JQMIGRATE: jQuery.browser is deprecated", source: https://info.bbdc.sg/wp-content/plugins/enable-j
query-migrate-helper/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (56)
[1213/220648.334:INFO:CONSOLE(58)] "console.trace", source: https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-helper/js/jq
uery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (58)
[1213/220648.354:INFO:CONSOLE(56)] "JQMIGRATE: jQuery.attrFn is deprecated", source: https://info.bbdc.sg/wp-content/plugins/enable-jq
uery-migrate-helper/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (56)
[1213/220648.355:INFO:CONSOLE(58)] "console.trace", source: https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-helper/js/jq
uery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (58)
[1213/220648.370:INFO:CONSOLE(56)] "JQMIGRATE: jQuery.fn.unload() is deprecated", source: https://info.bbdc.sg/wp-content/plugins/enab
le-jquery-migrate-helper/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (56)
[1213/220648.370:INFO:CONSOLE(58)] "console.trace", source: https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-helper/js/jq
uery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (58)
[1213/220649.013:INFO:CONSOLE(23)] "JQMIGRATE: Migrate is installed with logging active, version 1.4.1", source: https://info.bbdc.sg/
wp-content/plugins/enable-jquery-migrate-helper/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (23)
[1213/220649.030:INFO:CONSOLE(56)] "JQMIGRATE: jQuery.fn.load() is deprecated", source: https://info.bbdc.sg/wp-content/plugins/enable
-jquery-migrate-helper/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (56)
[1213/220649.030:INFO:CONSOLE(58)] "console.trace", source: https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-helper/js/jq
uery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (58)
[1213/220649.040:INFO:CONSOLE(825)] "Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')", source: https:/
/info.bbdc.sg/members-login/ (825)
[1213/220649.044:INFO:CONSOLE(56)] "JQMIGRATE: jQuery.browser is deprecated", source: https://info.bbdc.sg/wp-content/plugins/enable-j
query-migrate-helper/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (56)
[1213/220649.044:INFO:CONSOLE(58)] "console.trace", source: https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-helper/js/jq
uery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (58)
[1213/220649.062:INFO:CONSOLE(56)] "JQMIGRATE: jQuery.attrFn is deprecated", source: https://info.bbdc.sg/wp-content/plugins/enable-jq
uery-migrate-helper/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (56)
[1213/220649.063:INFO:CONSOLE(58)] "console.trace", source: https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-helper/js/jq
uery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (58)
[1213/220649.086:INFO:CONSOLE(56)] "JQMIGRATE: jQuery.fn.unload() is deprecated", source: https://info.bbdc.sg/wp-content/plugins/enab
le-jquery-migrate-helper/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (56)
[1213/220649.087:INFO:CONSOLE(58)] "console.trace", source: https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-helper/js/jq
uery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (58)
[1213/220657.962:INFO:CONSOLE(23)] "JQMIGRATE: Migrate is installed with logging active, version 1.4.1", source: https://info.bbdc.sg/
wp-content/plugins/enable-jquery-migrate-helper/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (23)
[1213/220657.977:INFO:CONSOLE(56)] "JQMIGRATE: jQuery.fn.load() is deprecated", source: https://info.bbdc.sg/wp-content/plugins/enable
-jquery-migrate-helper/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (56)
[1213/220657.977:INFO:CONSOLE(58)] "console.trace", source: https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-helper/js/jq
uery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (58)
[1213/220657.979:INFO:CONSOLE(385)] "Mixed Content: The page at 'https://info.bbdc.sg/members-login-redirect/' was loaded over a secur
e connection, but contains a form that targets an insecure endpoint 'http://www.bbdc.sg/bbdc/bbdc_web/header2.asp'. This endpoint shou
ld be made available over a secure connection.", source: https://info.bbdc.sg/members-login-redirect/ (385)
[1213/220657.993:INFO:CONSOLE(388)] "Mixed Content: The page at 'https://info.bbdc.sg/members-login-redirect/' was loaded over a secur
e connection, but contains a form that targets an insecure endpoint 'http://www.bbdc.sg/bbdc/bbdc_web/header2.asp'. This endpoint shou
ld be made available over a secure connection.", source: https://info.bbdc.sg/members-login-redirect/ (388)
http://www.bbdc.sg/bbdc/b-mainframe.asp
Traceback (most recent call last):
  File "C:\Users\Jonathan\PycharmProjects\BBDCBOT\BBDC_trial_headless.py", line 33, in <module>
    WebDriverWait(browser, 10).until(EC.url_matches('http://www.bbdc.sg/bbdc/bbdc_web/header2.asp'))
  File "C:\Users\Jonathan\venvs\automation\lib\site-packages\selenium\webdriver\support\wait.py", line 89, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

【问题讨论】:

这answer 对您有帮助吗? 【参考方案1】:

对于 selenium 4 所需的功能替换为浏览器选项。

from selenium.webdriver.chrome.options import Options
 
browser_options = Options()
browser_options.headless = True
driver = webdriver.Chrome(executable_path='G:\chromedriver\chromedriver.exe',options=browser_options)  

【讨论】:

我试过这个,但它不起作用。它有我的代码超时异常。只是想知道,这段代码是否与 webdriverwait 和 EC 一起使用?另外,我在上面的问题中添加了代码和异常。谢谢! 在您的日志中有从 https 到 http 的重定向。尝试添加browser_options.add_argument("--allow-running-insecure-content") 嘿@Max Daroshchanka,我已经添加了它,但它仍然会导致同样的错误 现在我不知道为什么它在非无头模式下工作。我也可以说它与方式无关,如何启用无头模式。无头模式以正确的方式启用。我可能会要求您在 webdriver 等待之前添加一些调试日志。您能否记录/打印browser.current_url 以了解在出错时打开了什么网址? 我已将 print(URL) 添加到代码中,并在上述问题中对其进行了更新。这导致它为第一个打印命令打印“bbdc.sg/bbdc/b-mainframe.asp”,但意识到第二个命令没有带来任何东西。另外,请注意,在我进入主页面之前,有一张“您要提交的信息不安全”页面的图片,该页面恰好是我将评估的第三个 URL。我在上面的问题中添加了照片。【参考方案2】:

我知道两种方法,它们都很好用。

    使用add_argument headless

    options = webdriver.ChromeOptions()
    options.add_argument("--headless")
    driver = webdriver.Chrome(options = options)
    

    使用.headless = True

    options = webdriver.ChromeOptions()
    options.headless = True
    driver = webdriver.Chrome(options = options)
    

【讨论】:

以上是关于Selenium是啥的主要内容,如果未能解决你的问题,请参考以下文章

selenium打开浏览器是啥端口

selenium 键盘操作中 的kw 是啥

Selenium 中的 TestNG 和 JUnit 框架是啥

Selenium是啥

请问一下selenium 里面的 点击鼠标右键函数是啥?

在 Selenium 2 中截取测试屏幕截图的最佳方法是啥?