基于 Selenium 的 Python 自动化测试框架 seleniumbase
https://seleniumbase.io/integrations/docker/ReadMe/
https://seleniumbase.io/
def mymain(url="https://hpo.jax.org/data/ontology"): ''' SeleniumBase常用命令列表详见: SeleniumBase/help_docs/method_summary.md 报下面这个错时,可以在/etc/hosts中添加 127.0.0.1 localhost试试 selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service /usr/local/lib/python3.10/dist-packages/seleniumbase/drivers/chromedriver 找到元素后,单击元素 robot_label = sb.find_element("label.altcha-label") sb.click(robot_label) ''' # with SB(headless=True , binary_location="/usr/bin/google-chrome") as sb: with SB(headless=True) as sb: sb.open(url) print(f"firt url: {url}") # 下载目录 download_folder = sb.get_downloads_folder() print(download_folder) ''' 保存当前html file_name = "wmba.html" sb.save_page_source(file_name) 也可利用save_screenshot截图保存 筛选超链接,按超链接在界面上显示的内容查找 sb.wait_for_element("a:contains('Parent Directory')", timeout=1000) ''' sb.wait_for_ready_state_complete() sb.sleep(2) # 给动态内容一点加载时间 items = sb.find_elements('h3[class*="download-banner-title"]') for item in items: print(item.text) item.click() print('11111', sb.get_current_url()) sb.sleep(2) # 获取下载目录 sfd = sb.get_downloaded_files() print(sfd) ''' 阻塞等待 抛出断言异常,测试失败 ''' sb.assert_downloaded_file("hp.json", timeout=60) # hp.obo hp.owl hp.json # 3. 获取文件路径并进行后续处理 file_path = sb.get_path_of_downloaded_file("hp.json") print(f"文件已保存至: {file_path}")
from seleniumbase import SB
def exec(url='https://pubmed.ncbi.nlm.nih.gov/39627211/'):
with SB(headless=True) as sb:
sb.open(url)
print(f"firt url: {url}")
# sb.wait_for_element('.heading-title')
doi_selector1 = '//*[@id="full-view-identifiers"]/li[3]/span/a'
_doi = sb.get_text(doi_selector1)
print(_doi)
# full_text_div = sb.find_elements('//*[@id="full-view-identifiers"]/li[3]/span/a')
_title = sb.get_text(".heading-title")
print(f"_title: {_title}")
if __name__ == "__main__":
exec()
制作镜像:
git clone http://github.com/seleniumbase/SeleniumBase.git
cd SeleniumBase
docker build -t seleniumbase .
SeleniumBase:功能全面的浏览器自动化框架。该项目是基于 Selenium 的 Python 自动化测试框架,集成了爬虫、自动化测试和生成报告等多种功能。它提供了丰富的示例,并且独特的 UC 模式,可以帮助开发者在进行浏览器自动化操作时避免被检测出来。
from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)
class TestSimpleLogin(BaseCase):
def test_simple_login(self):
self.open("seleniumbase.io/simple/login")
self.type("#username", "demo_user")
self.type("#password", "secret_pass")
self.click('a:contains("Sign in")')
self.assert_exact_text("Welcome!", "h1")
self.assert_element("img#image1")
self.highlight("#image1")
self.click_link("Sign out")
self.assert_text("signed out", "#top_message")
地址:github.com/seleniumbase/SeleniumBase
浙公网安备 33010602011771号