GVKun编程网logo

Selenium Webdriver:使用相对于其他元素的路径进行页面工厂初始化?(selenium中web页面元素的定位方式)

6

对于想了解SeleniumWebdriver:使用相对于其他元素的路径进行页面工厂初始化?的读者,本文将是一篇不可错过的文章,我们将详细介绍selenium中web页面元素的定位方式,并且为您提供关于

对于想了解Selenium Webdriver:使用相对于其他元素的路径进行页面工厂初始化?的读者,本文将是一篇不可错过的文章,我们将详细介绍selenium中web页面元素的定位方式,并且为您提供关于c# – 使用Selenium 2的IWebDriver与页面上的元素进行交互、Java+selenium之WebDriver定位页面元素(二)、Java+selenium之WebDriver页面元素的操作(三)、JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测的有价值信息。

本文目录一览:

Selenium Webdriver:使用相对于其他元素的路径进行页面工厂初始化?(selenium中web页面元素的定位方式)

Selenium Webdriver:使用相对于其他元素的路径进行页面工厂初始化?(selenium中web页面元素的定位方式)

我正在尝试使用页面工厂@FindBy注释在Selenium
Webdriver中编写页面对象。该页面对象用于边栏,并且以这种方式初始化了包含该页面对象需要与之交互的所有元素的父WebElement:

@FindBy (xpath = "//div[contains(@class,''yui3-accordion-panel-content'') and child::div[.=''Sidebar'']]")WebElement sidebar;

然后,我想要相对于此sidebar元素的搜索输入。有没有办法做这个参考sidebar元素?我可以将整个路径复制并粘贴到开头:

@FindBy (xpath = "//div[contains(@class,''yui3-accordion-panel-content'') and child::div[.=''Sidebar'']]//input[@id=''search'']")

但是我宁愿使它相对于第一个元素。这样有可能吗?

@FindBy (parent = "sidebar", xpath = ".//input[@id=''search'']")

在对@FindBy注释selenium文档是有点欠缺......

答案1

小编典典

回答我自己的问题。

答案是实现一个ElementLocatorFactory,该元素允许您提供搜索上下文(含义是驱动程序或WebElement)。

public class SearchContextElementLocatorFactory        implements ElementLocatorFactory {    private final SearchContext context;    public SearchContextElementLocatorFactory(SearchContext context) {        this.context = context;    }    @Override    public ElementLocator createLocator(Field field) {        return new DefaultElementLocator(context, field);    }}

然后,在实例化页面对象时,使用此定位器工厂。

WebElement parent = driver.findElement(By.xpath("//div[contains(@class,''yui3-accordion-panel-content'') and child::div[.=''Sidebar'']]"));SearchContextElementLocatorFactory elementLocatorFactory = new SearchContextElementLocatorFactory(parent);PageFactory.initElements(elementLocatorFactory, this);

现在您的@FindBy注释将相对于parent。例如,获取主侧边栏WebElement

@FindBy(xpath = ".")WebElement sideBar;

c# – 使用Selenium 2的IWebDriver与页面上的元素进行交互

c# – 使用Selenium 2的IWebDriver与页面上的元素进行交互

我正在使用Selenium的IWebDriver在C#中编写单元测试.

这是一个例子:

IWebDriver defaultDriver = new InternetExplorerDriver();
var ddl = driver.FindElements(By.TagName("select"));

最后一行检索包含在IWebElement中的select HTML元素.

我需要一种方法来模拟选择列表中的特定选项,但我无法弄清楚如何做到这一点.

大约research,我找到了人们使用ISelenium DefaultSelenium类来完成以下操作的示例,但我没有使用这个类,因为我正在使用IWebDriver和INavigation(来自defaultDriver.Navigate()).

我还注意到ISelenium DefaultSelenium包含许多其他方法,这些方法在IWebDriver的具体实现中不可用.

那么有什么方法可以将IWebDriver和INavigation与ISelenium DefaultSelenium结合使用?

解决方法

正如ZloiAdun所提到的,OpenQA.Selenium.Support.UI命名空间中有一个可爱的新类.这是访问选择元素的最佳方式之一,它的选项因为api非常简单.假设你有一个看起来像这样的网页
<!DOCTYPE html>
<head>
<title>disposable Page</title>
</head>
    <body >
        <select id="select">
          <option value="volvo">Volvo</option>
          <option value="saab">Saab</option>
          <option value="mercedes">Mercedes</option>
          <option value="audi">Audi</option>
        </select>
    </body>
</html>

你访问select的代码看起来像这样.注意我如何通过将普通的IWebElement传递给它的构造函数来创建Select对象. Select对象上有很多方法. Take a look at the source获取更多信息,直到获得正确记录.

using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium;
using System.Collections.Generic;
using OpenQA.Selenium.IE;

namespace Selenium2
{
    class SelectExample
    {
        public static void Main(string[] args)
        {
            IWebDriver driver = new InternetExplorerDriver();
            driver.Navigate().GoToUrl("www.example.com");

            //note how here's i'm passing in a normal IWebElement to the Select
            // constructor
            Select select = new Select(driver.FindElement(By.Id("select")));
            IList<IWebElement> options = select.Getoptions();
            foreach (IWebElement option in options)
            {
                System.Console.WriteLine(option.Text);
            }
            select.SelectByValue("audi");

            //This is only here so you have time to read the output and 
            System.Console.ReadLine();
            driver.Quit();

        }
    }
}

但是有关支持类的一些注意事项.即使您下载了最新的测试版,支持DLL也不会存在.支持包在Java库中有相对较长的历史(这是PageObject所在的历史),但在.Net驱动程序中它仍然很新鲜.幸运的是,从源代码构建起来非常容易.我pulled from SVN然后从测试版下载引用了WebDriver.Common.dll,并在C#Express 2008中内置.这个类没有像其他一些类那样经过良好测试,但我的例子在Internet Explorer和Firefox中工作.

根据上面的代码,我应该指出一些其他的事情.首先是您用来查找select元素的行

driver.FindElements(By.TagName("select"));

将找到所有选择元素.你应该使用driver.FindElement,而不是’s’.

此外,您很少直接使用INavigation.您将完成大部分导航,例如driver.Navigate().GoToUrl(“http://example.com”);

最后,DefaultSelenium是访问旧版Selenium 1.x apis的方法. Selenium 2与Selenium 1有很大的不同,所以除非你试图将旧测试迁移到新的Selenium 2 api(通常称为WebDriver api),否则你不会使用DefaultSelenium.

Java+selenium之WebDriver定位页面元素(二)

Java+selenium之WebDriver定位页面元素(二)

Selenium-Webdriver 提供了强大的元素定位方法,支持以下三种方法: 单个对象的定位方法,多个对象的定位方法和层级定位

1. 定位单个元素

 1 // 对于元素的属性包含 id 的情况适用,推荐使用
 2 WebElement we = drive.findElement(By.id("id"));
 3 // 对于元素的属性包含 name 的情况适用,推荐使用
 4 WebElement we = drive.findElement(By.name("name"));
 5 // 通用定位方式,对于不好定位的元素适用 ,推荐使用
 6 WebElement we = drive.findElement(By.xpath("xpathExpression"));
 7 // 适用于链接类的元素,即 <a> 标签,包含 herf 属性的元素
 8 WebElement we = drive.findElement(By.linkText("linkText"));
 9 // 适用于链接类的元素,模糊匹配,即包含 herf 属性的元素
10 WebElement we = drive.findElement(By.partialLinkText("linkText"));
11 // 对于元素的属性包含 class 的情况适用, 处理复合型class(中间有空格的classname)的方法 ,处理方法只选择class属性中的一个值
12 WebElement we = drive.findElement(By.className("className"));
13 // 通用定位方式,对于不好定位的元素适用
14 WebElement we = drive.findElement(By.cssSelector("selector"));
15 // 通用定位方式,用于层级定位
16 WebElement we = drive.findElement(By.tagName("tagName"));

2. 定位多个元素

findElements()方法可以返回一个符合条件的元素 List 

1 // 定义了一个集合,将 class 一样的元素一次性定位出来
2 List<WebElement> elements = driver.findElements(By.className("className"));
3 // 通过集合的下标进行操作,集合的第一位下标是0 ,参数index 就是下标
4 elements.get(index).click();

3. 层级定位

  层级定位的思想是先定位父元素,然后再从父元素中精确定位出其我们需要选取的子元素。层级定位一般的应用场景是无法直接定位到需要选取的元素,但是其父元素比较容易定位,通过定位父元素再遍历其子元素选择需要的目标元素,或者需要定位某个元素下所有的子元素。比较典型的应用是表格的定位。

1 // 得到 table 元素对象   
2 WebElement table = driver.findElement(By.id("id"));   
3 // 得到 table 表中所有行对象,并得到所要查询的行对象。   
4 List<WebElement> rows = table.findElements(By.tagName("tr")); 
5 // 定位下拉列表
6 WebElement select = driver.findElement(By.id("id")); 
7 // 从下拉列表中查找所有的选项
8 List<WebElement> option = a.findElements(By.tagName("option"));

 

Java+selenium之WebDriver页面元素的操作(三)

Java+selenium之WebDriver页面元素的操作(三)

1. 输入框(text field or textarea)

1 WebElement we = driver.findElement(By.id("id"));
2 //将输入框清空
3 we.clear();
4 // 在输入框中输入内容
5 we.sendKeys(“test”);
6 // 获取输入框的文本内容, 取得就是 value 属性的值 
7 element.getAttribute("value");

2. 下拉选择框(select)

 1 // 找到下拉选择框的元素
 2 Select select = new Select(driver.findElement(By.id("id"))); 
 3 // 选择对应的选择项
 4 select.selectByVisibleText(“ 北京市 ”); // 通过可见文本去选择
 5 select.selectByValue(“beijing”); // 通过 html 标签中的 value 属性值去选择
 6 select.selectByIndex(1); // 通过 index(索引从0开始)选择
 7 // 不选择对应的选择项
 8 select.deselectAll(); 
 9 select.deselectByValue(“ 替换成实际的值 ”); 
10 select.deselectByVisibleText(“ 替换成实际的值 ”); 
11 // 获取所有选择项的值
12 List<WebElement> wes = select.getAllSelectedOptions();
13 // 获取第一个选择项或者默认选择项
14 String text = select.getFirstSelectedOption().getText();

3. 单选框(Radio Button)

1 // 找到单选框元素
2 WebElement we =driver.findElement(By.id("id")); 
3 // 选择某个单选项
4 we.click(); 
5 // 清空某个单选项
6 we.clear(); 
7 // 判断某个单选项是否已经被选择, 返回的是 Boolean 类型
8 we.isSelected();

4. 多选框(Checkbox)

 1 // 找到多选框元素
 2 WebElement checkbox = driver.findElement(By.id("id")); 
 3 // 点击复选框
 4 checkbox.click();
 5 // 清除复选
 6 checkbox.clear();
 7 // 判断复选框是否被选中
 8 checkbox.isSelected();
 9 // 判断复选框是否可用
10 checkbox.isEnabled(); 

5. 按钮(Button)

1 // 找到按钮元素 
2 WebElement saveButton = driver.findElement(By.id("id")); 
3 // 点击按钮 
4 saveButton.click(); 
5 // 判断按钮是否可用 
6 saveButton.isEnabled ();

6. 左右选择框

1 // 左边是可供选择项,选择后移动到右边的框中,反之亦然,先处理选择框
2 Select lang = new Select(driver.findElement(By.id("languages"))); 
3 lang.selectByVisibleText(“English”);
4 // 再处理向右移动的按钮
5 WebElement addLanguage = driver.findElement(By.id("addButton")); 
6 addLanguage.click();

7. 弹出对话框(Popup dialogs)

1 // 切换到弹出框 
2 Alert alert = driver.switchTo().alert();
3 // 确定
4 alert.accept();
5 // 取消或者点"X"
6 alert.dismiss();
7 // 获取弹出框文本内容 
8 alert.getText();

8. 表单(Form)

1 // 只适合表单的提交
2 driver.findElement(By.id("approve")).submit();

 9. 上传文件 (Upload File)

1 // 定位上传控件
2 WebElement adFileUpload = driver.findElement(By.id("id"));
3 // 定义了一个本地文件的路径 
4 String filePath = "C:\\test\\uploadfile\\test.jpg";
5 // 为上传控件进行赋值操作,将需要上传的文件的路径赋给控件
6 adFileUpload.sendKeys(filePath);

10. 拖拉(Drag and Drop)

1 // 定义第一个元素
2 WebElement element =driver.findElement(By.name("source"));
3 // 定义第二个元素
4 WebElement target = driver.findElement(By.name("target"));
5 // 将第一个元素拖拽到第二个元素
6 (new Actions(driver)).dragAndDrop(element, target).perform();

 

11. 鼠标悬停(Mouse MoveOn)

1 Actions builder = new Actions(driver);
2 builder.moveToElement(driver.findElement(By.id("id"))).perform();

 

JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测

JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测

如何解决JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测?

从当前的实现开始,一种理想的访问网页而不被检测到的方法是使用ChromeOptions()该类向以下参数添加几个参数:

排除enable-automation开关的集合 关掉 useAutomationExtension 通过以下实例ChromeOptions

Java示例:

System.setProperty("webdriver.chrome.driver", "C:\\Utility\\browserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.setExperimentalOption("useAutomationExtension", false);
WebDriver driver =  new ChromeDriver(options);
driver.get("https://www.google.com/");

Python范例

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option(''useAutomationExtension'', False)
driver = webdriver.Chrome(options=options, executable_path=r''C:\path\to\chromedriver.exe'')
driver.get("https://www.google.com/")

解决方法

我正在尝试使用selenium和铬在网站中自动化一个非常基本的任务,但是以某种方式网站会检测到铬是由selenium驱动的,并阻止每个请求。我怀疑该网站是否依赖像这样的公开DOM变量https://stackoverflow.com/a/41904453/648236来检测selenium驱动的浏览器。

我的问题是,有没有办法使navigator.webdriver标志为假?我愿意尝试修改后重新尝试编译selenium源,但是似乎无法在存储库中的任何地方找到NavigatorAutomationInformation源https://github.com/SeleniumHQ/selenium

任何帮助深表感谢

PS:我还从https://w3c.github.io/webdriver/#interface尝试了以下操作

Object.defineProperty(navigator,''webdriver'',{
    get: () => false,});

但是它仅在初始页面加载后更新属性。我认为网站会在执行脚本之前检测到变量。

关于Selenium Webdriver:使用相对于其他元素的路径进行页面工厂初始化?selenium中web页面元素的定位方式的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于c# – 使用Selenium 2的IWebDriver与页面上的元素进行交互、Java+selenium之WebDriver定位页面元素(二)、Java+selenium之WebDriver页面元素的操作(三)、JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测等相关内容,可以在本站寻找。

本文标签: