How to use the By function from selenium-webdriver

Find comprehensive JavaScript selenium-webdriver.By code examples handpicked from public code repositorys.

selenium-webdriver.By is an object used to create locator strategies for web elements in WebDriver tests.

56
57
58
59
60
61
62
63
64
65
return function() {
  driver.get(`http://localhost:10114/dist/${url}`);
  return new Promise(function(resolve, reject) {
    driver
      .wait(function() {
        var readyroot = webdriver.By.css('html.ts-ready'); // not needed?
        return driver.findElements(readyroot);
      })
      .then(function() {
        currenturl = url;
fork icon42
star icon34
watch icon41

10
11
12
13
14
15
16
17
18
19
const chromedriver = require('chromedriver')
const log = require('npmlog')

// shortcuts
const until = webdriver.until
const By = webdriver.By

/* -----------------------------------------------------------------------------
 * configure
 * -------------------------------------------------------------------------- */
fork icon26
star icon428
watch icon3

How does selenium-webdriver.By work?

selenium-webdriver.By is a class that represents a mechanism to locate a web element within the DOM structure of a web page using different strategies such as id, name, class name, CSS selector, xpath, etc. When creating a By instance, you pass a locator strategy as the first argument and a value as the second argument. The By instance can then be used with a WebDriver instance to find elements on a web page that match the specified locator. For example, By.id("myId") creates a By instance that can be used to find an element with the id attribute set to "myId".

26
27
28
29
30
31
32
33
34
35
const throttle = require('../lib/throttle.es6.js');
const NameRewriter = require('object-graph-js').NameRewriter;
const stringify = require('ya-stdlib-js').stringify;
const getBrowsers = require('../lib/get_browsers.es6.js');

const By = webdriver.By;
getBrowsers.then(browsers => {
  throttle(5, browsers.map(browser => {
    return _ => {
      const logger = loggerModule.getLogger(browser);
fork icon10
star icon18
watch icon6

+ 3 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
const { Builder, By } = require("selenium-webdriver");

(async function example() {
  const driver = await new Builder().forBrowser("chrome").build();

  try {
    await driver.get("https://www.example.com");
    const element = await driver.findElement(By.className("example-class"));
    await element.click();
  } finally {
    await driver.quit();
  }
})();

In this example, the By.className method is used to locate an element with the class name 'example-class'. Once the element is located, its click() method is called to simulate a click action.

3
4
5
6
7
8
9
10
11
12
var stackTrace = require('stack-trace');
var LogManager = require('./logManager');
var Helper = require('./helper');

//simplify webdriver usage
global.by = webdriver.By;
global.key = webdriver.Key;
global.promise = webdriver.promise;
global.bot = webdriver.error;
global.until = webdriver.until;
fork icon8
star icon134
watch icon14

+ 3 other calls in file

146
147
148
149
150
151
152
153
154
155
                return waitForElement(func, errMsg, timeout);
        }
}

function waitForElement(css, errMsg, timeout) {
        var locator = webdriver.By.css(_.isString(css) ? css : css.selector);
        return driver.wait(
                webdriver.until.elementLocated(locator),
                //new webdriver.until.WebElementCondition(errMsg || 'wait() timeout', () => driver.findElement(locator)),
                timeout || WAIT_TIMEOUT,
fork icon3
star icon0
watch icon1

12
13
14
15
16
17
18
19
20
21

//imports
var path = require('path');
global.tf.projectDir = path.join(__dirname, '..');
var webdriver = require('selenium-webdriver'),
    By = webdriver.By;
var webdriverRemote = require('selenium-webdriver/remote');
var sprintf = require('sprintf-js').sprintf;
var config = require('./config.js');
var pageUrlData = require(global.tf.projectDir + '/data/pageUrlData.js');
fork icon2
star icon0
watch icon1

52
53
54
55
56
57
58
59
60
61
  "&origin=FACETED_SEARCH"
);
await tab.sleep(100000);
//search-results__total
let resText = await (await tab.findElement(
  swd.By.xpath("/html/body/div[5]/div[3]/div[2]/div/div[1]/main/div/div/div[1]/h2")
)).getText();
let results = resText.split(" ")[0];
results = results.replace(",", "");
results = parseInt(results);
fork icon0
star icon0
watch icon1

+ 7 other calls in file

101
102
103
104
105
106
107
108
109
110
};
CollapsibleWebElement.prototype.getToggleButton = function () {
    if (!this.toggleButton) {
        this.toggleButton = this.findElement(
        // Need to use Xpath to partial-match header Id
        selenium_webdriver_1.By.xpath('//*[starts-with(@id, "oj-collapsible-header-")]//button'));
    }
    return this.toggleButton;
};
CollapsibleWebElement.prototype.isExpanded = function () {
fork icon0
star icon0
watch icon0

1
2
3
4
5
6
7
8
9
10

let webdriver = require('selenium-webdriver');
let clientSideScripts = require('./clientsidescripts');


// Explicitly define webdriver.By.
export class WebdriverBy {
  className: (className: string) => By = webdriver.By.className;
  css: (css: string) => By = webdriver.By.css;
  id: (id: string) => By = webdriver.By.id;
fork icon0
star icon0
watch icon35

+ 95 other calls in file

14
15
16
17
18
19
20
21
22
23

/**
 * webdriver's By is an enum of locator functions, so we must set it to
 * a prototype before inheriting from it.
 */
WebdriverBy.prototype = webdriver.By;
util.inherits(ProtractorBy, WebdriverBy);

/**
 * Add a locator to this instance of ProtractorBy. This locator can then be
fork icon0
star icon0
watch icon26

+ 35 other calls in file