selenium 页面经常改变元素,元素“ id”位于在使用Selenium进行自动化测试期间检查元素时,某些网页中的更改(不是静态的)。如何处理这种情况?...

I am trying to capture a drop-menu field using test automation in SELENIUM with chrome driver.

Why does the element "id" changes in some web pages when inspecting elements time to time, with Chrome browser? How to keep the "id"s static, without changing?

Steps I followed:

When I inspect elements in the web page, the particular drop-menu shows its "id" as: id="combo-1782-inputEl"

HTML:

id observed by inspecting the web page in normal chrome browser:

f2507b6cb764094922385b30d3ca8e61.png

Then I used the above id in my java code(automation script) as below:

driver.findElement(By.id("combo-1782-inputEl")).click();

When I run the test > The google chrome browser opens automatically > The test gets successful till it meets the above line of code.

But, when it meets the above code line, the test failed Throwing the following exception:

class org.openqa.selenium.NoSuchElementException *

Then I inspected the same drop menu item in chrome web page opened (controlled) by automated test software, and found out that the "id" is different than the previous id mentioned in step 1.

The "id" in this case is: "combo-1781-inputEl"

id observed by inspecting the web page in chrome browser controlled by automated software:

f0d7b3cca44d6ec3daceea8bdb5fab74.png

As you can see, the number in the middle of the id has reduced from 1.

(1782-1 = 1781)

Same issue was found in the other drop menu items on the same web page.

What is the issue cause for this? How can I overcome this situation? Please help. :)

P.S. When I used "combo-1781-inputEl" ("id" from step 5) in my code, the test passed successfully.

driver.findElement(By.id("combo-1782-inputEl")).click(); //Test: failed

driver.findElement(By.id("combo-1781-inputEl")).click(); //Test: passed

I expected the test to be passed when I used the "id" I got in step 1 by inspecting the web page in normal chrome browser which is not controlled by automated software.

解决方案

In order to provide you with the best answer, I'd need to see a section of HTML code for the web page, enough that I can see other attributes. When you have dynamic ID's you have two options:

Option 1: use an xpath that uses part of the ID that is constant, i.e.:

//*[starts-with(@id, 'combo-')]

might do the trick, but only if there are no other similar ID's.

Or perhaps:

//*[starts-with(@id, 'combo-') and ends-with(@id, '-inputEl')]

but that might still not be specific-enough. This is why seeing a section of your HTML would help.

Option 2: use other attributes instead of ID, i.e. class, text, or some other attribute.

//*[@name='FirstName']

for example. You can craft rather elaborate xpaths using combinations of attributes, and it will be fairly stable if you do it right. Sharpening your xpath creation skills will come in handy for things like this.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值