代码拉取完成,页面将自动刷新
from time import sleep
import pytest
from Selenium4R import webdriver
from selenium.webdriver.common.by import By
from webdriver_helper import get_webdriver
@pytest.fixture()
def driver():
driver = get_webdriver("firefox")
# driver = webdriver.Edge()
driver.set_window_size(1920, 1080)
# driver.maximize_window()
# driver.minimize_window()
# driver.fullscreen_window()
return driver
def test_baidu_01(driver):
# with get_webdriver() as driver:
# driver = webdriver.Edge()
driver.get('https://www.baidu.com/')
url = driver.current_url # 本行用于获取当前页面的url,即百度首页地址
print(url)
def test_baidu_02(driver):
# with get_webdriver() as driver:
# driver = webdriver.Edge()
driver.get('https://www.baidu.com/')
driver.find_element(By.ID, 'kw').send_keys('selenium') # 搜索框输入selenium
driver.find_element(By.ID, 'su').click() # 点击百度一下
sleep(3)
driver.back() # 返回
def test_baidu_03(driver):
# driver = webdriver.Edge()
driver.get('https://www.baidu.com/')
driver.find_element(By.ID, 'kw').send_keys('selenium') # 搜索框输入selenium
driver.find_element(By.ID, 'su').click() # 点击百度一下
driver.back() # 返回
sleep(3)
driver.forward() # 前进
def test_baidu_04(driver):
# driver = webdriver.Edge()
driver.get('https://www.baidu.com/')
driver.refresh() # 刷新页面
def test_baidu_04(driver):
# driver = webdriver.Edge()
driver.get('https://www.baidu.com/')
title = driver.title # 获取当前页面title
print(title)
def test_baidu_05(driver):
driver.get('https://www.baidu.com/')
element = driver.find_element(By.ID, 'kw') # 通过ID定位搜索框
element.send_keys('selenium') # 输入selenium 验证定位是否成功
sleep(2)
driver.quit()
def test_baidu_06(driver):
driver.get('https://www.baidu.com/')
element = driver.find_element(By.NAME, 'wd') # 通过name定位搜索框
print(element)
def test_baidu_07(driver):
driver.get('https://www.baidu.com/')
element = driver.find_element(By.CLASS_NAME, 's_ipt') # 获取百度首页搜索框
print(element)
def test_baidu_08(driver):
driver.get('https://www.baidu.com/')
element = driver.find_element(By.TAG_NAME, 'input') # 查找标签名称是input的元素
print(element)
def test_baidu_08(driver):
driver.get('https://www.baidu.com/')
element = driver.find_element(By.LINK_TEXT, '贴吧') # 定位贴吧
element.click()
print(element)
sleep(2)
driver.quit()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。