Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >硒FindElement函数的通用方法

硒FindElement函数的通用方法
EN

Stack Overflow用户
提问于 2017-04-13 13:03:26
回答 3查看 2.7K关注 0票数 0

因此,我创建了这个通用的find元素函数:

代码语言:javascript
运行
AI代码解释
复制
    public static IWebElement FindElement(IWebDriver driver, Func<IWebDriver, IWebElement> expectedCondtions, int timeoutInSeconds)
    {
        WebDriverWait webDriverWait = CreateWebDriverWait(driver, finder,timeoutInSeconds);
        webDriverWait.IgnoreExceptionTypes(typeof(NoSuchElementException));
        return webDriverWait.Until(expectedCondtions);
    }

    public static ReadOnlyCollection<IWebElement> FindElements(IWebDriver driver, Func<IWebDriver, ReadOnlyCollection<IWebElement>> expectedCondtions, int timeoutInSeconds)
    {           
        WebDriverWait webDriverWait = CreateWebDriverWait(driver, finder, timeoutInSeconds);
        webDriverWait.IgnoreExceptionTypes(typeof(NoSuchElementException));            
        return webDriverWait.Until(expectedCondtions);
    } 

    private static WebDriverWait CreateWebDriverWait(IWebDriver driver, IWebElement finder, int timeoutInSeconds)
    {
        WebDriverWait webDriverWait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
        webDriverWait.IgnoreExceptionTypes(typeof(NoSuchElementException));
        return webDriverWait;
    }

用法:

代码语言:javascript
运行
AI代码解释
复制
        IWebElement element=
            WaitAndFindElement(
            driver,
            ExpectedConditions.ElementIsVisible(By.CssSelector("...")),
            120);

现在,我想添加一个选项,以查找也不使用elementdriver。例如,我想从另一个driver.FindElement中搜索元素,而不是element

代码语言:javascript
运行
AI代码解释
复制
IWebElemen element = ...
element.FindElement...

因此,我想将我的函数签名更改为:

代码语言:javascript
运行
AI代码解释
复制
IWebElement FindElement(IWebDriver driver,Func<IWebDriver, IWebElement> expectedCondtions, int timeoutInSeconds)

至:

代码语言:javascript
运行
AI代码解释
复制
IWebElement FindElement(IWebDriver driver, IWebElement finder, Func<IWebDriver, IWebElement> expectedCondtions, int timeoutInSeconds)

如果finder为null,我希望使用driver.FindElement进行搜索。否则:finder.FindElement

所以我的问题是如何做到这一点?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-04-14 02:51:23

代码语言:javascript
运行
AI代码解释
复制
Class WebElementFinder
{
   public static IWebElement FindElement(ISearchContext sc, By locator, Func<IWebElement, bool> elementCondition = null, int timeOutInceconds = 20)
        {
            DefaultWait<ISearchContext> wait = new DefaultWait<ISearchContext>(sc);
            wait.Timeout = TimeSpan.FromSeconds(timeOutInceconds);
            wait.PollingInterval = TimeSpan.FromSeconds(3);
            wait.IgnoreExceptionTypes(typeof(NoSuchElementException));

            return  wait.Until(x => GetElement(x, locator, elementCondition));
        }

        private static IWebElement GetElement(ISearchContext sc, By locator, Func<IWebElement, bool> elementCondition = null)
        {
             IWebElement webElement = sc.FindElement(locator);
             if(elementCondition != null)
            {
                if (elementCondition(webElement))
                    return webElement;
                else
                    return null;
            }
             else
            {
                return webElement;
            }
        }
}

用法:

代码语言:javascript
运行
AI代码解释
复制
 Func<IWebElement, bool> isElementVisible = (webElement) => webElement.Displayed;
            var element = FindElement(driver, By.Id("name_10"), isElementVisible);
票数 1
EN

Stack Overflow用户

发布于 2017-04-13 14:19:19

这很好:我在工作中遇到过类似的情况,这就是我在C#中所做的:

代码语言:javascript
运行
AI代码解释
复制
IWebElement FindElement(IWebDriver driver, Func<IWebDriver, IWebElement> expectedCondtions, int timeoutInSeconds, IWebElement finder = null)

这意味着"finder“的默认值为null,您可以使用或不指定它来调用函数。(可以向所有方法添加具有默认值的此参数)

然后,您可以在函数中简单地执行一个简单的if语句,以确定如何找到元素。

代码语言:javascript
运行
AI代码解释
复制
if(finder != null)
{
    //use finder instead of driver and return
}
//Otherwise use driver
票数 0
EN

Stack Overflow用户

发布于 2017-04-13 21:18:56

我认为如果你想把它变得通用的话,你不需要让它变得复杂。下面是解决这个问题的代码

代码语言:javascript
运行
AI代码解释
复制
Class WebElementFinder
{
 private static IWebElement FindElement(ISearchContext sc, By locator, int timeOutInceconds = 20)
        {
            DefaultWait<ISearchContext> wait = new DefaultWait<ISearchContext>(sc);
            wait.Timeout = TimeSpan.FromSeconds(timeOutInceconds);
            wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
            return wait.Until(x => GetElement(x, locator));
        }

        private static IWebElement GetElement(ISearchContext sc, By locator)
        {
           return sc.FindElement(locator);
        }
}

让我解释一下这段代码的某些部分,它将帮助您理解您提到的代码的问题。

  1. 如果您使用webdriver,那么它是从DefaultWait类驱动的,它将IWebDriver转换为泛型类型。因此,直到方法将对使用驱动程序对象有限制。
  2. 如果在方法中使用Func,那么每个调用方法都需要为find元素创建函数。但是,您可以使用它作为可选参数。

现在回到我的代码片段。我使用了DefaultWait,它既可以用于WebDriver,也可以用于IWebElement。两者都是从ISearchContext驱动的。按照您在文章中的要求,定位器将帮助在驱动程序/ IWebElement对象上查找web元素。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43401676

复制
相关文章
struts2的result重定向到另一个action
struts2 的 action执行后一般会转到某一 jsp,但有时候需要跳到某一 action,一般会用如下方法:   <result name="success" type="chain">actionName</result>   <result name="success" type="chain">actionName</result> 或者    <result name="success" type="redirect">actionName.action</result>   <resul
yawn
2018/03/14
1.1K0
iptable端口重定向 MASQUERADE[通俗易懂]
首先简述下NAT服务器在负载均衡中做了什么,简单的说就是Linux (内核2.4以后是Netfilter肩负起这个使命滴)内核缓冲区修改来源,目标地址。
全栈程序员站长
2022/07/02
12.4K0
iptable端口重定向 MASQUERADE[通俗易懂]
网络端口的转发和重定向(Python)
    需要将某个网络端口转发到另一个主机(forwarding),但可能会是不同的端口(redirecting)。
py3study
2020/01/09
1.5K0
【Python】重定向 Stream 到
Python 系统模块 sys 中有三个变量 stdin 、 stdout 与 stderr ,分别对应标准输入流、输出流与错误流。stdin 默认指向键盘, stdout 与 stderr 默认指向控制台。
py3study
2020/01/17
9450
Nginx配置https, 80端口重定向443
server { listen 443 ssl; server_name 域名; charset utf-8; access_log /var/log/nginx/webhook.iminho.me/access.log; add_header X-Xss-Protection 1; ssl_certificate /etc/nginx/cert/证书.pem; ssl_certificate_key /etc/nginx/cert/证
Wyc
2019/04/25
5.7K0
简单端口映射、转发、重定向工具-Rinetd
Rinetd是为在一个Unix和Linux操作系统中为重定向传输控制协议(TCP)连接的一个工具。Rinetd是单一过程的服务器,它处理任何数量的连接到在配置文件etc/rinetd中指定的地址/端口对。尽管rinetd使用非闭锁I/O运行作为一个单一过程,它可能重定向很多连接而不对这台机器增加额外的负担。
py3study
2020/03/07
6.6K0
Nginx 重定向所有子域名到www
vim .htaccess  或  vim  /var/www/html/.htaccess
阳光岛主
2019/02/18
6.6K0
Nginx 重定向所有子域名到www
SQL 复制表到另一个表
 INSERT INTO targetTableName SELECT COLUMNS FROM sourceTableName;
星哥玩云
2022/08/18
1K0
istio: http 流量 301重定向到 https
下面截取本站 Gateway 配置中的一部分,只需添加最后两行即可实现流量重定向。
SRE扫地僧
2021/10/07
2.8K0
istio: http 流量 301重定向到 https
go :复制文件内容到另一个文件
本文实验,从一个文件拷贝文件内容到另外一个文件 代码 package main import ( "fmt" "io" "os" ) func copyFileContents(src, dst string) (err error) { in, err := os.Open(src) if err != nil { return } defer in.Close() out, err := os.Create(dst)
IT工作者
2022/07/22
5920
nginx设置http 301重定向到https
  今天有位客户问ytkah在nginx服务器如何设置http 301重定向到https,其实不难。他的服务器安装宝塔面板了,更好操作了。进入站点设置的配置文件,如下图所示,在第11行左右加入跳转代码  
ytkah
2020/03/25
12.6K0
STM32中重定向printf到SWO口[通俗易懂]
引用网址:http://blog.csdn.net/xiaolei05/article/details/8526021
全栈程序员站长
2022/11/16
2.5K0
STM32中重定向printf到SWO口[通俗易懂]
Nginx代理HTTPS到Docker指定端口
假设我在服务器上的 Docker 运行了一个应用,在 Docker 启动的时候,我指定他监听了 localhost 的 9000 端口,定向到 Docker 的 9002 的应用上。
凝神长老
2020/04/17
1.8K0
Laravel Api表单验证失败被重定向到主页
Laravel Api 开发中,需要实现表单验证,但发现了一个问题,在 Laravel 中,api开发实现表单验证,如果验证失败,会被302重定向到主页。
Petrochor
2022/06/07
8040
Laravel Api表单验证失败被重定向到主页
如何将 Linux 命令输出重定向到文件?
在Linux系统中,命令行是非常强大和灵活的工具。它允许我们执行各种任务和操作,包括将命令的输出保存到文件中。本文将介绍如何使用重定向操作符将Linux命令的输出导入到文件中,并列举尽可能多的命令示例。
网络技术联盟站
2023/08/03
2.2K0
如何将 Linux 命令输出重定向到文件?
Linux之将目录bind到另一个目录
    注意,这个和软连接是不一样的. 记录下. List-1 [xx@xxxx]# more /etc/fstab ... 目录A 目录B none rw,bind 0 0 ...     这俩个目录要手动创建,之后执行mount -a     这样写入目录B其实操作就是目录A
克虏伯
2019/12/31
1.5K0
QT应用编程: QDebug输出重定向到日志文件
初始化QDebug输出重定向到日志文件,重定向之后,程序里通过qDebug()<<"xxx"输出的数据都会保存到在日志文件中;程序发布之后方便查看日志文件.了解程序执行情况。
DS小龙哥
2022/01/07
2.9K0
MINIFILTER实现文件重定向之从分析到实现
本次实验的测试环境为Windows Server 2008 R2 X64下。 为了解决例如系统关键目录或者业务敏感目录被放入恶意的可执行程序或者网页文件等,一些安全软件会使用文件过滤驱动的技术结合一定
FB客服
2018/02/09
2.9K0
MINIFILTER实现文件重定向之从分析到实现
如何将 Linux 命令输出重定向到文件?
在Linux系统中,命令行是非常强大和灵活的工具。它允许我们执行各种任务和操作,包括将命令的输出保存到文件中。本文将介绍如何使用重定向操作符将Linux命令的输出导入到文件中,并列举尽可能多的命令示例。
网络技术联盟站
2023/07/14
2K0
如何将 Linux 命令输出重定向到文件?
PortBender:一款功能强大的TCP端口重定向工具
PortBender是一款功能强大的TCP端口重定向工具,该工具允许红队研究人员或渗透测试人员将一个TCP端口(例如445/TCP)的入站流量重定向到另一个TCP端口(例如8445/TCP)。
FB客服
2021/12/06
2.2K0
PortBender:一款功能强大的TCP端口重定向工具

相似问题

等待正在运行的容器的正确异步方式

14

异步等待同步运行

10

异步/等待同步运行?

47

在Openshift中运行特权docker容器

14

运行容器内的Openshift 3 CronJob

20
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档