Java 需要关于动态CSS选择器的帮助吗

Java 需要关于动态CSS选择器的帮助吗,java,eclipse,selenium,css-selectors,Java,Eclipse,Selenium,Css Selectors,需要关于动态CSS选择器的帮助 对于所需的web元素,我有以下CSS选择器: #sub-cat-822 > div:nth-child(1) > div:nth-child(1) > form:nth-child(1) > div:nth-child(3) > div:nth-child(1) > div:nth-child(2) > input:nth-child(2) 我需要传递变量(变量名为:Added\u Main\u category\u i

需要关于动态CSS选择器的帮助

对于所需的web元素,我有以下CSS选择器:

#sub-cat-822 > div:nth-child(1) > div:nth-child(1) > form:nth-child(1) > div:nth-child(3) > div:nth-child(1) > div:nth-child(2) > input:nth-child(2)
我需要传递变量(变量名为:Added\u Main\u category\u id1),该变量只包含cat-822(在上述CSS选择器中)的动态值。请有人建议如何调整上述CSS选择器中的变量,使其能够顺利运行

所需web元素的外部html:

<input required="required" id="edited_name" name="sub_cate" value="" placeholder="" class="form-control" type="text">
html body.nav-md.pace-done.modal-open div.container.body div.main_container div.right_col div.right_col_container div.right_col_wrapper div.x_panel div.x_content div.post-box ul.cat_accordion.list-group li#854.list-group-item.category-list-item-854 div.cat-name div#sub-cat-854.modal.fade.in div.modal-dialog div.modal-content form div.modal-body.clearfix div.edit-photo-poup div.col-xs-8.col-sm-6 input#edited_name.form-control
  #sub-cat-822 > div:nth-child(1) > div:nth-child(1) > form:nth-child(1) > div:nth-child(3) > div:nth-child(1) > div:nth-child(2) > input:nth-child(2)
//*[@id="edited_name"]
css所需web元素的选择器:

<input required="required" id="edited_name" name="sub_cate" value="" placeholder="" class="form-control" type="text">
html body.nav-md.pace-done.modal-open div.container.body div.main_container div.right_col div.right_col_container div.right_col_wrapper div.x_panel div.x_content div.post-box ul.cat_accordion.list-group li#854.list-group-item.category-list-item-854 div.cat-name div#sub-cat-854.modal.fade.in div.modal-dialog div.modal-content form div.modal-body.clearfix div.edit-photo-poup div.col-xs-8.col-sm-6 input#edited_name.form-control
  #sub-cat-822 > div:nth-child(1) > div:nth-child(1) > form:nth-child(1) > div:nth-child(3) > div:nth-child(1) > div:nth-child(2) > input:nth-child(2)
//*[@id="edited_name"]
所需web元素的xpath:

<input required="required" id="edited_name" name="sub_cate" value="" placeholder="" class="form-control" type="text">
html body.nav-md.pace-done.modal-open div.container.body div.main_container div.right_col div.right_col_container div.right_col_wrapper div.x_panel div.x_content div.post-box ul.cat_accordion.list-group li#854.list-group-item.category-list-item-854 div.cat-name div#sub-cat-854.modal.fade.in div.modal-dialog div.modal-content form div.modal-body.clearfix div.edit-photo-poup div.col-xs-8.col-sm-6 input#edited_name.form-control
  #sub-cat-822 > div:nth-child(1) > div:nth-child(1) > form:nth-child(1) > div:nth-child(3) > div:nth-child(1) > div:nth-child(2) > input:nth-child(2)
//*[@id="edited_name"]

您真的需要使用CSS选择器而不是Xpath吗?通常Xpath在动态场景中有帮助。请参阅下面的代码

String searchText = "text in the webtable";
driver.get("http://some.site.having.webtable/");
WebElement e = driver.findElement(By.xpath("//td[text()='" + searchText + "']"));
然后可以根据需要定制xpath


请发布您的HTML以简化操作。

如果id“edited_name”在HTML源代码中是唯一的,只需使用
#edited_name
作为CSS选择器

您可以将选择器字符串视为普通字符串。您可以在其中插入任何想要的变量等

你说下面的方法有效

Invoke_Test_Instance.driver.findElement(By.cssSelector("#sub-cat-831 > div:nth-child(1) > div:nth-child(1) > form:nth-child(1) > div:nth-child(3) > div:nth-child(1) > div:nth-child(2) > input:nth-child(2)")).sendKeys("input value");
您可以使用类似于

String id = "#sub-cat-831";
Invoke_Test_Instance.driver.findElement(By.cssSelector(id + " > div:nth-child(1) > div:nth-child(1) > form:nth-child(1) > div:nth-child(3) > div:nth-child(1) > div:nth-child(2) > input:nth-child(2)")).sendKeys("input value");
然后将
id
变量更改为所需的任何字符串

也就是说。。。拥有这么多级别的CSS选择器或任何定位器可能会非常脆弱。由于级别的数量以及您使用的索引,DOM的任何级别的任何更改都将破坏该定位器。

下面的逻辑成功工作。感谢Jeffc分享这个想法。

字符串category\u id=添加的\u Main\u category\u id 1//包含#cat-906动态值的变量

字符串截断\u类别\u id

截断的_category_id=category_id.substring(category_id.lastIndexOf(“#”)+1);//移除#并获取其余动态值(即cat-906)

字符串id=“#子-”+截断的#类别#id

睡眠(3000)


调用\u Test\u Instance.driver.findElement(By.cssSelector(id+)>div:n子项(1)>div:n子项(1)>form:n子项(1)>div:n子项(3)>div:n子项(1)>div:n子项(2)>input:n子项(2)”).sendKeys(“字段输入值”);//作为cssselector传递动态变量和其他静态值

发布HTML。我什么也看不到。把你的问题推到它上面。拉特米尔·阿萨诺夫,你能分享一下你到底需要什么吗?把你的HTML内部问题贴上缩进。想要的元素有一个ID,你为什么不使用它<代码>驱动程序.findElement(By.id(“编辑的名称”)?666是的,我真的需要CSS选择器。。因为在我的场景中,我花了很多时间,静态cssselector可以正常工作,所以我只需要传递一个包含动态值的变量,剩余的cssselector始终保持不变。您可以通过编辑在原始帖子中发布HTML吗?或者更好的是只提供给我们检查的链接。亲爱的King666,正如你所建议的,我已经在我的问题中提供了外部html和csspath。期待我的问题得到解决。在创建动态定位器时,ThanksPath并不比CSS选择器更有优势。它们的工作原理相同,只是CSS选择器更快,支持范围更广,语法更容易学习。@JeffC谢谢。你能帮忙解决我的问题吗。谢谢你的回复。事实上,当我尝试使用cssselector中的#edited_name运行脚本时,我在执行后得到:[org.openqa.selenium.elementNotInteractitableException:Element不可见]错误,而如果我通过完整的cssselector:i.e.#sub-cat-854>div:n子项(1)>div:n子项(1)>表单:n子项(1),同一脚本将成功执行>div:nth child(3)>div:nth child(1)>div:nth child(2)>input:nth child(2)唯一的问题是我需要在cssselector中传递变量而不是cat-854,因为我的变量包含动态值。我使用cssselector是因为我在使用xpath时遇到了很多不同的异常(我想将我的变量与#sub-一起使用,或者你可以说#sub-+我的变量+组合是必需的,还有剩余的cssselector部分是静态的),这样我的脚本应该能够与dybamic cssselector一起运行。希望你能理解这一点。