1

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

1 Answer 1

2

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')
Sign up to request clarification or add additional context in comments.

2 Comments

Terribly efficient compared with diving into regex, +1 :)
@zx81 yeah, I wish the question would be tagged as regex :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.