Python Selenium 元素点击行为奇怪 [英] Python Selenium Element Click Behaving Strangeley

查看:43
本文介绍了Python Selenium 元素点击行为奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Chromedriver 加载网站并尝试点击基于文本的链接.

这是我要查找的链接的站点代码:

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

我正在尝试:

justtime = "14:10"链接 = Driver.find_element_by_partial_link_text(justtime)链接.点击

我正在手动观察浏览器,它在我的代码触发之前加载了数据(包括链接),所以它不像它不存在.

它抛出了一个没有此类元素的异常.但我很难理解为什么.我一直使用类似的代码,我唯一能想到的是网站编码不会使链接文本看起来像链接文本.

但我也试过

link = Driver.find_element_by_id("hr_selection_18359391")

得到了同样的结果.在这里有点难倒会很感激任何能阐明我做错了什么的人.

/更新:

正如下面指出的,find_element_by_partial_link_text 适用于标签而不是 div.尝试过:

find_element(By.PARTIAL_LINK_TEXT(justtime))link = Driver.find_element_by_xpath("//div[contains(text(), justtime))")

但那里也没有骰子.猜猜我将不得不找到另一种方法来做到这一点.

根据我正在阅读的内容,Xpath 包含的内容应该可以正常工作.甚至尝试过:

 link = Driver.find_element_by_xpath("//div[contains(.,'14:10 * ')]")链接.点击()

这给了我一个属性错误..我觉得有点奇怪.

/更深入的兔子洞:

所以我想可能有不止一个链接隐藏在某个地方.我试过了:

justtime = "14:10"links = Driver.find_elements_by_xpath("//div[contains(.,justtime)]")对于链接中的每个链接:打印 eachlink.text

它似乎吐出页面上的每个链接.即使这些 DIV 没有理由包含 14:10.所以我的 xpath 一定有问题,但我看不到什么.

解决方案

两件事:

find_element_by_partial_link_text

仅适用于 标签,不适用于

click 是一个函数,应该是

link.click()

对于find_element_by_id,如果元素在内,你需要先切换到它

frame = driver.find_element_by_id("id")driver.switch_to.frame(frame)# 继续代码link = Driver.find_element_by_id("hr_selection_18359391")链接.点击()

然后切换回来

driver.switch_to.default_content()

Loading a site with Chromedriver and trying to click a link based on the text.

This is the site code for the link I'm looking for:

<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>

And I'm trying:

justtime = "14:10"
link = Driver.find_element_by_partial_link_text(justtime)
link.click

I'm manually watching the browser it's loaded the data (including the link) before my code fires so it's not like it isn't there.

It's throwing a no such element exception. But I'm struggling to understand why. I use similar code all the time the only thing I can think is something with the site coding doesn't make the link text look like link text.

But I also tried

link = Driver.find_element_by_id("hr_selection_18359391")

And got the same result. Kind of stumped here would appreciate anyone who can shed some light on what I'm doing wrong.

/Update:

As pointed out below find_element_by_partial_link_text works on a tags not divs. Tried:

find_element(By.PARTIAL_LINK_TEXT(justtime))

link = Driver.find_element_by_xpath("//div[contains(text(), justtime))")

But no dice there either. Guess I'm going to have to find another way to do this.

According to what I'm reading Xpath contains should be working. Even tried:

  link = Driver.find_element_by_xpath("//div[contains(.,'14:10 *      ')]")
        link.click()

Which gave me an attribute error.. which I find a little weird.

/Further down the rabbit hole:

So I thought maybe there's more than one link hidden in there somewhere. I tried:

justtime = "14:10"
links = Driver.find_elements_by_xpath("//div[contains(.,justtime)]")
for eachlink in links:
 print eachlink.text

And it seemed to spit out every link the page had. Even though there's no reason for those DIVs to contain 14:10. So something must be wrong with my xpath but I can't see what.

解决方案

Two things:

find_element_by_partial_link_text

Is working only on <a> tags, it won't work on <div>

click is a function, it should be

link.click()

And for the find_element_by_id, if the element is inside <frame> you need to switch to it first

frame = driver.find_element_by_id("id")
driver.switch_to.frame(frame)
# continue with the code
link = Driver.find_element_by_id("hr_selection_18359391")
link.click()

And to switch back

driver.switch_to.default_content()

这篇关于Python Selenium 元素点击行为奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆