Unlocking subsequent matches with XPath in Selenium RC

I currently have a total of 20 labels displayed on my page:

In [85]: sel.get_xpath_count("//label")
Out[85]: u'20'

By default, I am able to retrieve the text from the first label like this:

In [86]: sel.get_text("xpath=//label")
Out[86]: u'First label:'

However, when attempting to access the text of the second label using xpath subscripting, I encounter an error. This is contrary to what the xpath documentation suggests:

In [87]: sel.get_text("xpath=//label[2]")
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (216, 0))

ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (1186, 0))

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)

/Users/me/<ipython console> in <module>()

/Users/me/selenium.pyc in get_text(self, locator)
   1187         'locator' is an element locator
   1188         """
-> 1189         return self.get_string("getText", [locator,])
   1190 
   1191 

/Users/me/selenium.pyc in get_string(self, verb, args)
    217 
    218     def get_string(self, verb, args):
--> 219         result = self.do_command(verb, args)
    220         return result[3:]
    221 

/Users/me/selenium.pyc in do_command(self, verb, args)
    213         #print "Selenium Result: " + repr(data) + "\n\n"

    214         if (not data.startswith('OK')):
--> 215             raise Exception, data
    216         return data
    217 

Exception: ERROR: Element xpath=//label[2] not found

Why am I facing this issue?

Answer №1

Try this:

(//label)[2]

Your current XPath query:

//label[2]

Explanation:

This new expression selects the second instance of a label element in the entire document. The previous query was specifying the second child label which might not have existed in your document structure.

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

Determine the total count of distinct combinations within a Pandas data frame

I seem to be facing some difficulties (mental block) when it comes to creating basic summary statistics for my dataset. What I am trying to accomplish is counting the instances of co-occurring "code" values across all "id"s. The data is structured as foll ...

Having difficulty accessing the VR Path Registry

Software Version: geckodriver-v0.20.0-win64 Operating System: Windows 10 Home Single Web Browser: Firefox: 59.0.2 (64-bits) Selenium Software: selenium-java-3.11.0 IDE: intelliJ Idea : 2018.1 Community Edition Greetings! I have recently started using We ...

The issue of Selenium crashing in Docker because the browsing context has been discarded

Looking for a solution on how to execute Selenium based tests within Docker? I'm experimenting with running Python+Selenium tests that utilize Firefox and Geckodriver in an Ubuntu 18 Docker container. The contents of my docker-compose.yml file are a ...

Selenium is having trouble locating elements on the webpage

Currently, I am creating Selenium tests using C#, Specflow, and Nunit for a new project I am involved in. Unfortunately, I am facing an issue where nothing inside the body can be selected under any circumstances. https://i.stack.imgur.com/627kf.png I am ...

extract the content of CSS pseudo-elements using Python or Selenium

Currently, I am working on automating a web service using Selenium and Python. My ultimate goal is to extract the text "test" located below. However, I am facing some challenges in figuring out if this is feasible through Selenium or any Python library. & ...

Guide to including only zeros and keeping non-zero values unchanged in Python's OpenCV

I need assistance with adding a shifted version of an image to the original, while ensuring zeros are left intact. I attempted the following code but encountered some issues. Any help is greatly appreciated! import cv2 import numpy as np #Given image i ...

Navigating through a Dropdown Menu in Selenium WebDriver while Allowing the Page to Fully Load

During my attempts to iterate through player seasons on NBA.com and extract shooting statistics after selecting each season from the dropdown menu, I encountered a persistent issue. Every time I click on a new season, an error message stating "list index o ...

Filtering Strings with Prefix Matching in Python using Regular Expressions

When it comes to Regular Expressions, things can get a bit tricky. For example, I recently delved into a kernel on Kaggle for the Titanic dataset. In this dataset, there is a field containing the names of passengers. #Exploring the data and looking for r ...

By default in Excel, the value "TRUE" is recognized as a boolean "TRUE", causing a failure in my selenium code

I have a situation in my Excel Input where one of the cell values is "true". By default, Excel interprets this as "TRUE" - a boolean value. Even after trying to handle this in my code by using .toString(), it still fails to work properly. Are there any p ...

Preventing Selenium from immediately exiting and addressing issues with keys not being typed

Is anyone else experiencing the issue where the site opens for a split second and exits, and it doesn't type what it's supposed to? How can this be fixed? I've tried multiple methods to locate the element, and I believe my approach is corre ...

Encountering a 500 error when deploying a Flask app to Heroku stemming from issues with the create_app

After successfully running my app locally with "Flask run", I encountered an error when trying to deploy it on Heroku. The structure of my app is similar to the microblog flask tutorial with blueprints. Upon accessing the deployed website, I received the f ...

Unable to locate an extended hyperlink text using Selenium WebDriver

There is a challenge I am facing in identifying an element by link text. When the link text value is short, like addFirst or addLast, I am able to locate the element easily using: driver.findElement(By.linkText("addLast, addFirst")).click() However, when ...

Utilize a DataFrame with a MultiIndex structure to index rows and columns using values from another DataFrame that includes row and column indices

Within my dataset, I have a collection of particle pairs identified by a unique combination of chain-index and intra-chain-index for each particle. Storing this information in a DataFrame named index_array, I now aim to create a matrix representation of al ...

The REST API in Python encountered an error when attempting to cast a JSONArray to a JSONObject

I'm working on a python script that has two main tasks: Parse a CSV file Send the parsed data to a remote server using its REST API So far, my code successfully parses the CSV file and converts it into a JSON object. However, when I try to send th ...

Java programming language, Selenium WebDriver

Here is a snapshot of the Login popup: https://i.stack.imgur.com/iBal9.png I am new to Selenium webdriver and I have written some code to test navigation commands. However, when the browser opens, a login popup appears that I am unable to close using clas ...

Having trouble finding an element in a new tab on Chrome using Selenium Webdriver with Java

When you click the menu in the application, it automatically opens that page in a new tab and performs the action on the new tab String mainWindow = driver.getWindowHandle(); //when the element is clicked below, it opens in a new tab driver.findElement(B ...

Exploring Selenium With Various Network Connections

I have a Linux system with two different interfaces (such as eth0 and tap0). I'm trying to run two separate instances of selenium phantomjs browsers simultaneously, each using a different interface. Is it possible to choose which interface to use in s ...

What is the best way to make a python script repeat a set number of times continuously?

To repeat a Python script 10 times, I encountered an intentional error that halts the process. This occurs because the script interacts with a website where a random number of questions are generated, up to a maximum of 7. To handle this variability and pr ...

The peculiar phenomenon observed in Python3

calculate: print(int( 342342342342342342 / 10 )) The result of the calculation is 34234234234234236 I'm perplexed by the appearance of the 6 at the end of the number! I attempted using "long" which resulted in an error. ...

Using SymPy to substitute values into a variable's output

Within the context of my program, I've defined a variable h that holds the result of an integration. My goal is to implement a change in coordinates on the output of this integration process. To achieve this, I typically rely on computer algebra softw ...