Tips for creating a Single Xpath instead of Multiple paths

I'm currently working with Protractor alongside TypeScript. I have an element located at:

xpath = "**//div[@class='loglist']/div[1]/div[2]**"

Afterwards, there are additional elements that need to be identified in different divs such as:

**/div[2]/div[2]**, **/div[3]/div[2]**, /**div[4]/div[2]**, 

The only part changing is the div number. How can I combine all of these into a single xpath instead of having multiple xpaths?

Answer №1

Here's an example:

_selector = function (i, k) {
  return "//div[@class='loglist']/div[" + i + "]/div[" + k "]"
}
element(by.xpath(_selector(1,2)))

This code snippet is in javascript, but you can easily convert it to TypeScript.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountered Angular SSR Serve Error: NullInjectorError - StaticInjectorError in AppServerModule with the following reference:

While working on building an application with Angular's SSR and serving it, I encountered a specific error. All services and components have been properly injected. Error: ERROR Error [NullInjectorError]: StaticInjectorError(AppServerModule)[REQUEST] ...

A guide to using the up and down keys to switch focus between div elements in a react component with TypeScript and CSS

I am currently working with a scenario where data is being displayed within different div elements, and I wish to enable the selection/focus of a specific div when users use the up/down arrow keys. While trying to achieve this functionality by using refs ...

Unable to find the login element using Selenium

I'm having trouble finding the login on a webpage because the element isn't visible - I keep getting a NoSuchElementException. I've been using the following code, but I keep encountering the exception: WebDriverWait wait = new WebDriverWait ...

Encountering a "Duplicate identifier error" when transitioning TypeScript code to JavaScript

I'm currently using VSCode for working with TypeScript, and I've encountered an issue while compiling to JavaScript. The problem arises when the IDE notifies me that certain elements - like classes or variables - are duplicates. This duplication ...

The Javascript Node class encountered an error: X has not been defined

I have a class that looks like this: const MongoClient = require("mongodb").MongoClient; const ConnectionDetails = require("./ConnectionDetails").ConnectionDetails; const Recipe = require("./recipe").Recipe; var ObjectId = req ...

The ChromeDriver service that was set up earlier is currently operational / Executing automated tests with Grunt using Webdriver

Greetings! I am currently in the process of writing Mocha tests for my React application that integrates with selenium-webdriver. I have a couple of inquiries and any assistance would be greatly appreciated to help me progress further. Firstly, my ide ...

Encountering an issue in a Next.js application while building it, where an error is triggered because the property 'protocol' of 'window.location' cannot be destructured due to being undefined

While building my nextjs application, I encountered the following error. My setup uses typescript for building purposes, although I am only using JavaScript. Build error occurred: TypeError: Cannot destructure property 'protocol' of 'window ...

The absence of the libgfx library is causing a Chromium-Chromedriver issue on Raspberry Pi

Currently, I'm attempting to run Chromium in headless mode on a Raspberry Pi 3. After obtaining the Chromium-chromedriver package from this specific repository, I am encountering a persistent error. Whenever I make an attempt to execute chromium-chrom ...

Creating Custom Type Guards for Literal Types in Typescript: Is It Possible?

Note: I am new to using typescript. Before asking this question, I made sure to go through the documentation on advanced types and type guards. Additionally, I looked into several related questions on Stack Overflow such as user defined type guards [typesc ...

Error in TypeScript: Module 'stytch' and its corresponding type declarations could not be located. (Error code: ts(2307))

I'm currently developing a Next.js application and encountering an issue while attempting to import the 'stytch' module in TypeScript. The problem arises when TypeScript is unable to locate the module or its type declarations, resulting in t ...

The error message "Module 'electron' not found" is commonly encountered when working with Electron and TypeScript

Hey there! I'm having some trouble with Electron not supporting TypeScript on my setup. I'm using vscode 1.16.1 and here is an overview of my package.json: { [...] "devDependencies": { "electron": "^1.6.13", "ts-loader": "~2.3.7", ...

Transferring Selenium Cookies into CookieContainer

I attempted the following code snippet: foreach (OpenQA.Selenium.Cookie cook in driver.Manage().Cookies.AllCookies) { System.Net.Cookie cookie = new System.Net.Cookie(); cookie.Name = cook.Name; cookie.Value = cook.Value; cookie.Domain = c ...

Upgrading from Angular 2 to 4 causes compilation failure in project

Recently, I upgraded my Angular project from version 2 to version 4. The steps I followed for this upgrade are as follows: 1- Deleted the /node_modules/ folder 2- Executed the following command: npm install @angular/common@latest @angular/compiler@lat ...

What techniques does Angular2 use to handle dependency injection?

When working with Angular2 components, I know that to inject a dependency, you simply annotate an argument in the constructor, like how ThingService is injected here. However, what I am wondering is how Angular actually knows what to inject at runtime. A ...

The discord.js TypeScript is throwing an error stating that the 'index.ts' file is missing when trying to run 'ts-node index.ts'

I have been working on creating a discord bot using discord.js and TypeScript. However, when I attempt to start the bot by running 'ts-node index.ts', I encounter the following error: Error: Cannot find module 'node:events' Require stac ...

Tips for creating Junit tests for a CDK environment

As a newcomer to CDK, I have the requirement to set up SQS infrastructure during deployment. The following code snippet is working fine in the environment: export class TestStage extends cdk.Stage { constructor(scope: cdk.Construct, id: string, props: ...

Step-by-step guide on setting up automatic authentication when launching a web app in Firefox using Katalon Studio

https://i.sstatic.net/Yb7bz.png Currently, I am creating test cases with Katalon Studio. An issue I have encountered is that when running the test case on Firefox, it displays an "Authentication Required" popup. However, this does not happen when using Ch ...

Invoke an ActionCreator within a different ActionCreator

Calling an ActionCreator from another file is proving to be a challenge... The products.ts file contains the ActionCreators and Reducers for Products... import { setStock } from './Store.ts'; //.... export const addProduct = (product: IProduct) ...

Handling JSON data with Reactive Extensions in JavaScript

Hey everyone, I'm a beginner in Angular and RxJS coming from a background in VueJS. I've been struggling to grasp the inner workings of RxJS and would really benefit from some guidance from more experienced individuals regarding my current issue. ...

What is the solution when findElement() in WebDriver on Android using Appium cannot locate the element after sendKeys()?

My Android emulator and Appium test are running smoothly. The app launches the correct Activity and enters text into a specific field without any issues. However, when I attempt to locate the same text field in order to verify its content, I receive an err ...