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!
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.
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'].
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.
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.
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)')]')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With