selenium - 如何使用Webdriver Selenium获取 “style”元素的值

标签 selenium selenium-webdriver

我有以下 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/

相关文章:

selenium-webdriver - 如何使用 RemoteWebDriver 设置浏览器语言

java - 如何消除页面对象模型中重复的 WebElement

java - 使用 Selenium WebDriver 按下按钮

selenium - 即使 Chrome 版本匹配,ChromeDriver v80 也不起作用 (Chrome v80)

java - 当窗口尚未加载任何内容时,如何在新窗口上访问警报?

java - 确定 FirefoxProfile 是否接受不受信任的证书

java - 在 Selenium 中执行 findElement 时未找到元素

testing - 如何在 Jenkins 中使用 Selenium Webdriver 运行 UI 测试类?

regex - 如何更改我的正则表达式,以便将其正确应用于我尝试抓取的 URL?

python - 如何拍摄 Selenium 中发生的事情的视频