python - Python + Selenium --How to find a dynamic element
问题描述
I'm a newbie in Selenium. I'm trying to click on an element, HTML structure looks like:
<label>
<span> text </span>
</label>
I need to click on "text" under <span>
, this whole thing is inside another span/div/body etc. and I'm trying to make it dynamic so I can locate it with a different text as well.
And I'm using below code for it:
[...]
vote = input("Which option should I vote for you? : ")
option = "//label/span[contains(.,'" + vote + "')]\""
option = str(option)
[...]
browser.find_element_by_xpath(option).click()
解决方案
您可以使用 CSS 选择器,但是在不知道 DOM 其余部分的结构的情况下,这可能不够独特,无法可靠地抓取您想要的元素:
find_element_by_css_selector("label span")
推荐阅读
- matlab - .fig 到 .eps 转换中的问题 (MATLABR 2015b)
- intellij-idea - 我如何配置 intellij idea + jrebel + liferay dxp
- javascript - Netflix Falcor - 何时从数据源返回 pathValues 与 jsonGraph?
- bash - 使用 jq 计算 geoJSON 多边形的重心
- javascript - 在函数内的“”中转换 null
- android - 存储库类获取数据时如何通知活动?
- elasticsearch - 如何在复制控制器运行时更改集群 IP
- python - 当您将鼠标悬停在该点上时,标签中的 Plotly 链接会消失
- javascript - 从javascript中的选项框中选择并“单击”一个选项
- angular - ngrx Reducer - 返回新状态的正确方法