webdriver.Remote的用法

webdriver.Remote 是 Selenium WebDriver 的一个关键组件,它允许你远程连接到另一个机器上的 WebDriver 实例,从而执行浏览器自动化操作。这在需要跨多个环境或平台进行自动化测试时特别有用。

以下是 webdriver.Remote 的基本用法:

  1. 导入必要的库:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  1. 设置远程 WebDriver 的 URL:
    这个 URL 通常指向远程 WebDriver 服务器的地址和端口。例如,如果你使用的是 Appium,那么 URL 可能是类似于 http://localhost:4723/wd/hub 的东西。

  2. 创建 DesiredCapabilities 对象:
    这个对象用于描述你希望远程 WebDriver 实例使用的浏览器和配置。例如,你可以指定浏览器类型、版本、操作系统等。

capabilities = DesiredCapabilities.CHROME.copy()
# 你也可以在这里添加或修改 capabilities
  1. 初始化 Remote WebDriver:
    使用上面设置的 URL 和 DesiredCapabilities 对象来初始化 Remote WebDriver。

driver = webdriver.Remote(command_executor='http://localhost:4723/wd/hub', desired_capabilities=capabilities)
  1. 执行自动化操作:
    一旦你有了 Remote WebDriver 实例,你就可以像使用常规的 WebDriver 一样执行自动化操作了。例如,打开页面、点击元素、填写表单等。

driver.get("https://www.example.com")
element = driver.find_element_by_id("some-element-id")
element.click()
# ... 其他操作 ...
  1. 关闭 WebDriver:
    完成所有操作后,记得关闭 WebDriver 以释放资源。

driver.quit()

注意:为了使用 webdriver.Remote,远程机器上必须运行一个 WebDriver 服务器,并且你的机器必须能够通过网络连接到该服务器。此外,你还需要确保所有必要的 WebDriver 客户端和服务器库都已正确安装和配置。

如果你使用的是 Appium 进行移动应用测试,那么 webdriver.Remote 的用法会略有不同,因为你需要指定与移动应用相关的配置,如设备信息、应用包名等。具体用法可以参考 Appium 的官方文档。

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
自动化个人学习第一步笔记import os import time import logging import configparser from appium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait base_path=os.path.abspath(os.path.dirname(os.path.dirname(__file__))) class comm: @staticmethod def driver(): """APPIUM驱动""" desired_caps={} desired_caps['platformName']=comm.conf(section='desired_caps',key='platformName') # 手机操作系统 desired_caps['deviceName']=comm.conf(section='desired_caps',key='deviceName') # 手机设备号 desired_caps['platformVersion']=comm.conf(section='desired_caps',key='platformVersion') # 操作系统版本 desired_caps['appPackage']=comm.conf(section='desired_caps',key='appPackage') # app包名 desired_caps['appActivity']=comm.conf(section='desired_caps',key='appActivity') # app ACTIVITY名称 desired_caps['resetKeyboard']=True # 是否在测试结束后将键盘重轩为系统默认的输入法。 desired_caps['newCommandTimeout']=comm.conf(section='desired_caps',key='newCommandTimeout') # Appium服务器待appium客户端发送新消息的时间。默认为60秒 desired_caps['noReset'] = True # true:不重新安装APP,false:重新安装app desired_caps['automationName'] = comm.conf(section='desired_caps',key='automationName') # appium1.5以后的版本才支持toast定位 driver=webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps) return driver @staticmethod def get_element(driver,by,selector): """ description:封装定位方法;id指的是resource-id,class_name指的是class,uiautomator使用方法:'new UiSelector().text("文字内容")',xpath定位元素 uthor:clay date: 2020-5-08 params: driver 驱动,by 选择定位的方式,selector 定位的语法 """ if by == 'id': element=driver.find_element_by_id(selector) elif by =='class_name': element=driver.find_element_by_class_name(selector) elif by =='uiautomator': element=driver.find_elemen_by_android_uiautomator(selector) elif by == 'xpath': element=driver.find_element_by_xpath(selector) else: raise NameError return element @staticmethod def tap(driver,upper_left_x,upper_left_y,bottom_right_x,bottom_right_y,time): """ description:根据坐标触发点击事件 uthor:clay date: 2020-5-08 params: driver:驱动,upper_left_x:左上角坐标X轴,upprt_lef_y:左上角Y轴,bottom_right_x:右下角X轴,bottom_right_y:右下角Y轴,time:点击停留的时间 ms为单位 """ tap_=driver.tap([(upper_left_x,upper_left_y),(bottom_right_x,bottom_right_y)],time) return tap_ @staticmethod def tips(driver, text): """ descirption:xpath定位toast提示 author:supper date: 2029-10-06 params: text:元素的text文本 """ try: toast_loc = ("xpath", "//*[contains(@text,'%s')]" % text) toast = WebDriverWait(driver, 5, 0.1).until(EC.presence_of_element_located(toast_loc)) text = toast.text print('提示为:%s' % text) return True except: return False

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值