简体   繁体   English

如何使用 Selenium 在网页中搜索元素的修改?

[英]How to search for a modification of an element in a web page using Selenium?

I am a beginner with Cucumber/Selenium, and I have a web page on which there is a table on which one of the boxes can be 0 or 1. my problem is that the value of this box varies over time, I have tried many times to check that this element is 1 then 0 after a certain time, but I do not succeed in it.我是 Cucumber/Selenium 的初学者,我有一个网页,上面有一张表格,其中一个框可以是 0 或 1。我的问题是这个框的值随时间变化,我尝试了很多多次检查此元素在一段时间后是否为 1 然后为 0,但我没有成功。

I know how to get the css or xpath of this element, but it doesn't change when the value of this element changes, and no way to get the associated value (0 or 1) of this element.我知道怎么获取这个元素的css或者xpath,但是当这个元素的值改变时它不会改变,也没有办法获取这个元素的关联值(0或1)。

What I've tried:我试过的:

By inspecting the part of the source code associated with this element I saw that a character in the source code went from 1 to 0 at the same time as the box, and I tried to check this change with driver.getPageSource.contains ("the area around the source code that contains the changed character*"), after waiting a little with a wait instruction to allow the source code the time to change, but this has never worked.通过检查与此元素关联的源代码部分,我看到源代码中的一个字符与框同时从 1 变为 0,我尝试使用 driver.getPageSource.contains(“包含更改的字符 *") 的源代码周围的区域,在使用等待指令稍等片刻后,允许源代码有时间更改,但这从未奏效。

Anyone have an idea to help me do it right?任何人都有一个想法来帮助我做对吗? It is very important for me to find the solution to my problem,找到解决问题的方法对我来说非常重要,

Thank you.谢谢你。

If you want to get the text from an element, use:如果要从元素获取文本,请使用:

String value = driver.findElement(By.XPath(*your XPath here*)).getText();

This will output the inner text from the element at the moment you call it.这将在您调用元素时输出元素的内部文本。 If your element doesn't have any inner text, use:如果您的元素没有任何内部文本,请使用:

getAttribute("value")

When trying to get the value of an element, it is best practice that you specify what your element is and then try to get the value from it, rather than getting the entire page source and making sure what you expect is contained in it.在尝试获取元素的值时,最佳做法是指定元素是什么,然后尝试从中获取值,而不是获取整个页面源并确保其中包含您期望的内容。

There are many resources that will help you gain an understanding of how Selenium and Cucumber work, which I would recommend having a look through before trying to build a solution.有许多资源可以帮助您了解 Selenium 和 Cucumber 的工作原理,我建议您在尝试构建解决方案之前先浏览一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM