PoweredByCoffee
PoweredByCoffee

Reputation: 1193

Should be Simple XPATH?

Using Python and Selenium I'm trying to click a link if it contains text. In this case say 14:10 and this would be the DIV I'm after.

<div class="league_check" id="hr_selection_18359391" onclick="HorseRacingBranchWindow.showEvent(18359391);" title="Odds Available">                <span class="race-status">                    <img src="/i/none_v.gif" width="12" height="12" onclick="HorseRacingBranchWindow.toggleSelection(18359391); cancelBubble(event);">                </span>14:10 *            </div>

I've been watching the browser move manually. I know the DIV has loaded before my code fires but I can't figure out what the heck it is actually doing.

Looked pretty straightforward. I'm not great at XPATH but I usually manage the basics.

justtime = "14:10"
links = Driver.find_elements_by_xpath("//div*[contains(.,justtime)")

As far as I can see no other link on that page contains the text 14:10 but when I loop through links and print it out it's showing basically every link on that page.

I've tried to narrow it down to that class name and containing the text

justtime = "14:10"
links = Driver.find_elements_by_xpath("//div[contains(.,justtime) and (contains(@class, 'league_check'))]")

Which doesn't return anything at all. Really stumped on this it's making no sense to me at all.

Upvotes: 2

Views: 74

Answers (2)

har07
har07

Reputation: 89325

Currently, your XPath didn't make use of justtime python variable. Instead, it references child element <justtime> which doesn't exists within the <div>. Expression of form contains(., nonExistentElement) will always evaulates to True, because nonExistentElement translates to empty string here. This is possibly one of the reason why your initial XPath returned more elements than the expected.

Try to incorporate value from justtime variable into your XPath by using string interpolation, and don't forget to enclose the value with quotes so that it can be properly recognized as XPath literal string :

justtime = "14:10"
links = Driver.find_elements_by_xpath("//div[contains(.,'%s')]" % justtime)

Upvotes: 5

Kuldeep Yadav
Kuldeep Yadav

Reputation: 107

You have need to use wait for element

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID,'someid')))

Upvotes: 0

Related Questions