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

时间:2016-08-10 07:36:12

标签: python google-chrome selenium-webdriver

我正在尝试编写脚本,这样我就可以在Chrome的内部页面中搜索字符串(例如:" chrome:// help")。 /> 有没有一种简单的方法,或者它需要特殊的工具,(如Selenium webdriver API)? 我知道可以将它用于"正常"网页,但"内部"的?

1 个答案:

答案 0 :(得分:1)

您可以使用selenium Webdriver轻松完成此任务,在这种情况下,我们将提取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