我有以下 HTML 路径,我试图捕获“收件箱”值,但我无法使用 Selenium Webdriver 在 google chrome 中找到特定元素。
HTML 路径:
<div style="position: absolute; visibility: inherit; overflow: hidden; cursor: default; color: white; text-align: left; width: 84px; height: 14px; padding-left: 1px; padding-top: 1px; left: 1px; top: 1px; background-color: rgb(102, 0, 153);">Inbox"
<img src="/images/tridown.gif" style="position: absolute; width: 8px; height: 4px; top: 9px; left: 75px;">
</div>"Inbox"
<img src="/images/tridown.gif" style="position: absolute; width: 8px; height: 4px; top: 9px; left: 75px;">
</div>
我认为 left 和 top 是收件箱下拉列表的唯一标识符,所以您能帮助我们编写命令以从上面的 HTML 中获取“收件箱”值
最佳答案
正如 Michas 和 Saritha G 所建议的,您的 HTML 代码格式正确,但话虽如此。请使用下面的示例:
<div style="position: absolute; visibility: inherit; overflow: hidden; cursor: default; color: white; text-align: left; width: 84px; height: 14px; padding-left: 1px; padding-top: 1px; left: 1px; top: 1px; background-color: rgb(102, 0, 153);"></div>
如果你想检索上面元素的“style”属性值,你需要首先找到这个元素:
firefox = webdriver.Firefox()
element = firefox.find_element_by_css_selector("this element css selector here")
attributeValue = element.get_attribute("style")
然后 attributeValue 应具有以下字符串“position:absolute;可见性:继承;溢出:隐藏;光标:默认;颜色:白色;文本对齐:左;宽度:84px;高度:14px;填充-左:1px;上边距:1px;左:1px;上:1px;背景颜色:rgb(102, 0, 153);"
我使用Python作为示例。
关于selenium - 如何使用Webdriver Selenium获取 “style”元素的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31819312/