Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choose element by partial ID using Selenium with python?

I am trying to reach an web element in Selenium in Python 2.7. The element ID is like this:

cell.line.order(240686080).item(250444868).unitCost

The first number string '240686080' is known, but the second number '250444868' is unknown beforehand.

I tried to reach it by something like this.

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("somewebsite.com")
driver.find_elements_by_id('input.line.order(240417939).item('+ '\d{9}'+').unitCost')

So my questions is, is there anyway we can search and reach this element only with part of the ID known?

I found similar question answered below, but it was in C#. And unfortunately, I don't know C#.

Finding an element by partial id with Selenium in C#

Thank you in advance!

Edited 4/8

The element is coded like this:

<td id="cell.line.order(240417939).item(250159165).unitCost" class="or_monetarydata">
    <input id="input.line.order(240417939).item(250159165).unitCost" type="hidden" value="135.00" 
    name="order(240417939).item(250159165).unitcost"></input>

    135.00

    </td>

I could get the element list by

value=driver.find_elements_by_xpath("//*[contains(@id, 'input.line.order(240417939)')]")

Thank you!

like image 712
bing Avatar asked Apr 06 '13 00:04

bing


People also ask

Can we find element by partial ID match in Selenium?

We can locate elements by partial id match with Selenium webdriver. This can be achieved while we identify elements with the help of xpath or css locator. With the css and xpath expression, we use the regular expression to match id partially.

How do I select an element by ID in Selenium?

We can find an element using the attribute id with Selenium webdriver using the locators - id, css, or xpath. To identify the element with css, the expression should be tagname[id='value'] and the method to be used is By. cssSelector. To identify the element with xpath, the expression should be //tagname[@id='value'].

Where is Element ID in Selenium Python?

Python – find_element_by_id() method in Selenium In order to find element by ID find_element_by_id() method is used. Id is basically unique attribute assigned to the elements of the web page such as buttons, image, heading etc. Note :The first element with the ID attribute value matching the location will be returned.

How can we locate an element by only partially matching its attributes value in xpath?

We can identify elements by partially comparing to its attributes in Selenium with the help of regular expression. In xpath, there is contains () method. It supports partial matching with the value of the attributes. This method comes as useful while dealing with elements having dynamic values in their attributes.


1 Answers

Although the answer is in C#, the underlying solution is still the same, by using CSS selectors:

driver.find_elements_by_css_selector('input[id*='cell.line.order(240686080)']')

or XPath will also be able to do this:

driver.find_elements_by_xpath('//*[contains(@id, 'cell.line.order(240686080)')]')
like image 196
Arran Avatar answered Oct 16 '22 10:10

Arran