Reputation: 9253
I'd like to find the product ID (ProductID1111111) given the product name (Product 1) using Python. My thoughts would be to use the find function to return the character position of Product1 then back up a few characters, but I am sure there is a better way to do this.
HTML CODE:
<HTML>
<Table>
<TR>
<TD>
<a id="ProductID1111111" class="Products" href="some url">
<span id="ProductID1111111">Product 1</span></a>
<BR>
<a id="ProductID2222222" class="Products" href="some url">
<span id="ProductID2222222">Product 2</span></a>
</TD>
<TR>
</TABLE>
</HTML>
PYTHON:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get('someurl.com')
html = driver.page_source
Upvotes: 1
Views: 81
Reputation: 474171
Find the span
element by the xpath and call get_attribute()
to get the id
attribute value:
span = driver.find_element_by_xpath('//span[. = "Product 1"]')
product_id = span.get_attribute('id')
Upvotes: 2