应该是简单的XPATH?

Pow*_*fee 2 python selenium xpath web-scraping

使用Python和Selenium我试图点击链接,如果它包含文本.在这种情况下说14:10,这将是我追求的DIV.

<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>
Run Code Online (Sandbox Code Playgroud)

我一直在看手动浏览器.我知道在我的代码触发之前DIV已经加载但是我无法弄清楚它实际上在做什么.

看起来很简单.我不擅长XPATH,但我通常会管理基础知识.

justtime = "14:10"
links = Driver.find_elements_by_xpath("//div*[contains(.,justtime)")
Run Code Online (Sandbox Code Playgroud)

据我所知,该页面上没有其他链接包含文本14:10但是当我循环浏览链接并将其打印出来时,它基本上显示了该页面上的每个链接.

我试图将其缩小到该类名并包含文本

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

这根本不会返回任何东西.真的很难过,这对我来说毫无意义.

har*_*r07 5

目前,您的XPath没有使用justtimepython变量.相反,它引用了<justtime>不存在于其中的子元素<div>.表单的表达contains(., nonExistentElement)总是会被证实True,因为nonExistentElement这里转换为空字符串.这可能是您的初始XPath返回的元素多于预期的原因之一.

尝试justtime通过使用字符串插值将变量中的值合并到XPath中,并且不要忘记用引号括起该值,以便可以将其正确识别为XPath 文字字符串:

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