存储从 selenium 派生的浏览器标题
storing the browser title derived from selenium
我是硒的菜鸟。我选择 python 来学习自动化。这是我的问题。我一直在尝试做以下事情。
- 获取页面标题名称和标题长度
- 在 IDLE Console/any 控制台上打印页面标题和标题长度。
- 获取页面 URL 和 URL 长度
- 在 IDLE Console/any 控制台上打印 URL 和 URL 长度。
- 刷新当前页面
- 获取页面源代码(HTML源代码)和页面源代码长度
对于所有这些,我知道我可以从文档中获取 Web driver 绑定。但我很困惑如何将这些存储到数据类型中。我在 java 中看到了答案,但在 python 中没有看到。 Python高手能不能帮帮我。这是我一直在尝试的代码。
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.google.com") # Load page
curre = browser.current_url
print curre #this gives an error.
以下代码有效,我已经测试过(在 Python 2.7 上):
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox() # Get local session of firefox
# 0 wait until the pages are loaded
browser.implicitly_wait(3) # 3 secs should be enough. if not, increase it
browser.get("http://www.google.com") # Load page
# 1 & 2
title = browser.title
print title, len(title)
# 3 & 4
curre = browser.current_url
print curre, len(curre)
#5
browser.refresh()
#6
page_source = browser.page_source
print page_source, len(page_source)
我是硒的菜鸟。我选择 python 来学习自动化。这是我的问题。我一直在尝试做以下事情。
- 获取页面标题名称和标题长度
- 在 IDLE Console/any 控制台上打印页面标题和标题长度。
- 获取页面 URL 和 URL 长度
- 在 IDLE Console/any 控制台上打印 URL 和 URL 长度。
- 刷新当前页面
- 获取页面源代码(HTML源代码)和页面源代码长度
对于所有这些,我知道我可以从文档中获取 Web driver 绑定。但我很困惑如何将这些存储到数据类型中。我在 java 中看到了答案,但在 python 中没有看到。 Python高手能不能帮帮我。这是我一直在尝试的代码。
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.google.com") # Load page
curre = browser.current_url
print curre #this gives an error.
以下代码有效,我已经测试过(在 Python 2.7 上):
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox() # Get local session of firefox
# 0 wait until the pages are loaded
browser.implicitly_wait(3) # 3 secs should be enough. if not, increase it
browser.get("http://www.google.com") # Load page
# 1 & 2
title = browser.title
print title, len(title)
# 3 & 4
curre = browser.current_url
print curre, len(curre)
#5
browser.refresh()
#6
page_source = browser.page_source
print page_source, len(page_source)