russellaugust
russellaugust

Reputation: 358

Find partial content of text on page, then return the full contents of that <div> in Python with Selenium

I've been struggling with how to accomplish this.

I need Selenium to find the partial content of text on a page and give me the full content.

My page, somewhere on it says "http://some.website/r@nd0mlyg3neratedt3xt". We'll assume "r@nd0mlyg3neratedt3xt" is constantly changing to other names, but there's never more than one instance of it on the page at a time.

So I'd search for something like "http://some.website/", it would find where that exists on the page, and then return "http://some.website/r@nd0mlyg3neratedt3xt". The text I'm looking for would be the full content of the <div>.

Only way I could think was find the text, check the ID of the <div> it's in, return that ID, and then use the ID to return the text. But I couldn't figure out how to get the ID of the partial text's location.

EDIT: I should also point out that the ID has been randomly generated. So there's not much to grab on to.

Here's what I'm looking at in the page:

<div id="yxi_350111162360070455_2385" class="link wx-label"><span>http://some.website/r@nd0mlyg3neratedt3xt</span></div>

I'd like to basically get that link. some.website will never change, but the characters at the end do.

EDITED: Clarity, remove the Hello My Name Is part, and referencing the css directly.

Upvotes: 1

Views: 224

Answers (2)

lc123
lc123

Reputation: 158

driver.find_elements_by_xpath('//div[@class="ink wx-label"]/span[contains(text(), "http://some.website"]')

Upvotes: 0

Clandestine
Clandestine

Reputation: 1499

Assuming you know a selector that is specific enough to return at least a list of matching elements (e.g. a class named 'wx-label', as in your example), one of which has the text you are looking for, you can do the following to get the full text of the correct element:

#### Assume driver is your webdriver instance

text = 'http://some.website'
elements = driver.find_elements_by_class('wx-label')

for element in elements:
    if text in element.text:
        print element.text

Upvotes: 1

Related Questions