从WebElement中获取CSS选择器字符串在Selenium WebDriver - Java [英] Get CSS selector string from WebElement in Selenium WebDriver - Java

查看:779
本文介绍了从WebElement中获取CSS选择器字符串在Selenium WebDriver - Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WebElement,我只是试图提取CSS选择器字符串。这是我调试代码时的变量的值:


[[ChromeDriver:chrome on MAC(345345345n5435345b34)] - > css selector :
div [class ='alert alert-danger']]




我需要只能访问css选择器:

  div [class ='alert alert-danger']] 
pre>

我可以拆分字符串,但是有没有人知道Selenium提供的更有效的方法?我尝试使用 .getAttribute(class)它返回alert alert-danger,但我需要整个选择器。



感谢您阅读,任何帮助将非常感激。

解决方案

一个简单的方法是使用 regex 。从您提供的给定字符串,您可以使用以下regex表达式提取整个选择器:

  div。* 

请参阅 Regex101



有时候你有其他元素(不总是div),所以你可以创建一个方法,把它作为参数,所以你可以使用例如: ul。* 等。


I have a WebElement and I'm simply trying to extract the CSS selector string. This is the value of the variable when I debug the code:

[[ChromeDriver: chrome on MAC (345345345n5435345b34)] -> css selector: div[class='alert alert-danger']]

I need to have access to just the css selector:

div[class='alert alert-danger']]

I could split the string, but does anyone know of a more efficient method that Selenium provides? I've tried using .getAttribute("class") which returns "alert alert-danger" but I need the entire selector.

Thank you for reading, any help would be much appreciated.

解决方案

One simple way would be to use regex. From the given string you provided you could extract the whole selector by using the following regex expression:

div.*

See Regex101

Sometimes you have other elements (not always div), so you could create a method which takes this as an argument, so you can use the same for for example: ul.* etc.

这篇关于从WebElement中获取CSS选择器字符串在Selenium WebDriver - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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