Java 如何将selenium webdriver xpath值转换为css
当前代码具有长HTML Xpath值,需要将其转换并缩短为css值:Java 如何将selenium webdriver xpath值转换为css,java,css,selenium,xpath,webdriver,Java,Css,Selenium,Xpath,Webdriver,当前代码具有长HTML Xpath值,需要将其转换并缩短为css值: driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div/form/div[3]/div[2]/button")).click(); 您可能已经选择了比上面这个更好的xpath表达式。您所做的(不看实际的HTML代码)是您已经写下了完整的xpath,是否可以使其更短/更健壮 考虑以下示例: <html itemscope itemtype="http:
driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div/form/div[3]/div[2]/button")).click();
- 您可能已经选择了比上面这个更好的xpath表达式。您所做的(不看实际的HTML代码)是您已经写下了完整的xpath,是否可以使其更短/更健壮李>
<html itemscope itemtype="http://schema.org/QAPage">
<body class = "question-page new-topbar">
<div id="notify-container"></div>
<div id="topbar-wrapper"></div>
<button id="button1"></button>
</body>
</html>
或者,您可以使用xpath沿着该元素的其他属性(在本例中为其id)找到它
driver.findElement(By.xpath("//button[@id='button1']")).click()
或者根据需要,您可以使用CSS选择器:
driver.findElement(By.cssSelector('button[id='button1']')).click()
如果您希望我们帮助您将xpath转换为css选择器,您还需要在问题中复制并粘贴html代码。如果不看实际代码,我们无法100%确定
在尝试将xpath转换为css选择器时,您可能会发现以下链接很有用。
那么,你的问题是什么?是否收到错误消息?这不是一个好的xpath,它应该类似于
//表单[Identification form here]//按钮[Identification button here]
,并且它没有将其转换为CSS选择器的正确信息。首先将此xpath更改为使用@id
、@class
或其他唯一属性。请提供相关的HTML,以便我们可以帮助您确定CSS选择器。一些CSS引用可能对您有所帮助:,如果您有一个ID,您可以使用By.ID(“button1”)
而不是CSS选择器。
driver.findElement(By.xpath("//button[@id='button1']")).click()
driver.findElement(By.cssSelector('button[id='button1']')).click()