如何使用python和/或selenium在chrome内部页面中搜索字符串

2024-09-30 06:26:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个脚本,它允许我在Chrome的内部页面中搜索字符串(例如:chrome://帮助“)。
有没有一种简单的方法,或者它需要特殊的工具(比如seleniumwebdriverapi)?
我知道它可以用于“普通”网页,但“内部”网页呢?你知道吗


Tags: 工具方法字符串脚本网页页面chromeseleniumwebdriverapi
1条回答
网友
1楼 · 发布于 2024-09-30 06:26:44

您可以使用seleniumwebdriver轻松地完成此任务,在本例中,我们将提取Google Chrome的版本号。你知道吗

下面是示例代码,注释解释了每个步骤:

from selenium import webdriver

# executable path should be the place where chrome driver is on the computer

driver = webdriver.Chrome(executable_path= '/users/user/Downloads/Chromedriver')

# This line tells the driver to go to the help section of chrome

driver.get('chrome://help')

# Because certain elements are stored in another Iframe, you must switch to this particular Iframe which is called 'help' in this case.

driver.switch_to.frame(driver.find_element_by_name('help'))

# retrive the text of the element and store it's text in a variable.

version_string = driver.find_element_by_id('version-container').text

# Now you can easily print it.

print version_string

相关问题 更多 >

    热门问题