我目前正在使用 Splinter 构建一些 Web 自动化脚本,到目前为止还不错,但我遇到了一个问题,我实际上无法使用 Splinter 包装器函数修复它。但是,我相信我可能已经找到了一个可以使用Selenium Webdriver函数修复的解决方案,即使我主要使用Splinter。
我相信我几年前就已经这样做了,但是无论我在哪里搜索(文档,Google,Stackoverflow(,我似乎都找不到任何内容,所以也许它不再是一个功能了?
无论如何,基本上我需要访问Selenium网络驱动程序功能。
根据记忆,我相信代码是这样的:
browser = splinter.browser("firefox")
brower.visit("google.com")
browser.webdriver.find_element_by_id('loginForm') #This is calling the selenium web driver direcetly, not the Splinter find_by_id function.
.webdriver 。司机
两者似乎都不起作用。
有谁知道如何正确执行此操作?如果您需要更多信息,请告诉我。感谢您的帮助。
硒由浏览器属性driver
暴露:
>>> from splinter import Browser
b>>> b = Browser()
>>> b.driver
<selenium.webdriver.firefox.webdriver.WebDriver (session="e607ceaa-2c63-435e-9991-432376102bf5")>
>>>
此实现集成了webdriver_manager模块。
浏览器示例:
from splinter import Browser
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.service import Service as ChromiumService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType
executable_path = {'executable_path': ChromeDriverManager().install()}
driver = Browser('chrome', **executable_path, headless=False)
url = 'https://google.com'
driver.visit(url)
勇敢的浏览器示例:
from splinter import Browser
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as BraveService
from webdriver_manager.core.utils import ChromeType
executable_path = {'executable_path': ChromeDriverManager(chrome_type=ChromeType.BRAVE).install()}
option = webdriver.ChromeOptions()
option.binary_location = "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser"
driver = Browser('chrome', **executable_path, options=option, headless=False)
url = 'https://google.com'
driver.visit(url)
更新 Brave - 不再需要可执行路径参数:
option = webdriver.ChromeOptions()
option.binary_location = "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser"
driver = Browser('chrome', options=option, headless=False)
url = 'https://google.com'
driver.visit(url)
**注意:我只在Brave上测试过这个,Firefox示例我假设相同的模板。 火狐浏览器示例:
from splinter import Browser
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.firefox.service import Service as FirefoxService
executable_path = {'executable_path': GeckoDriverManager().install()}
option = webdriver.ChromeOptions()
option.binary_location = "/Applications/Firefox.app/Contents/MacOS/firefox"
driver = Browser('firefox', **executable_path, options=option, headless=False)
url = 'https://google.com'
driver.visit(url)
火狐选项: https://splinter-test.readthedocs.io/en/latest/drivers/firefox.html