Selenium自动化 – 使用ChromeDriver时ID不同

huangapple go评论53阅读模式
英文:

Selenium automation - ID is different when using ChromeDriver

问题

当我使用内部开发的应用程序手动访问网站(启动Chrome,输入URL等),并检查特定元素时,我看到该元素的ID属性如下:

id="input-145"

但是,当我使用Chromedriver和Selenium来运行我的测试时,同一元素显示的ID属性如下:

id="input-147"

有人曾经见过这种情况吗?我不确定如何继续进行。

我希望ID属性无论如何访问网站都保持不变。

英文:

Working with an in-house developed application, when I go to the web site manually (launch Chrome, type in the URL, etc.) and inspect a particular element, I see that element with the ID attribute as follows:

id="input-145"

When I use Chromedriver and Selenium to run my test, the same element shows up with this ID attribute:

id="input-147"

Has anyone ever seen this before? I'm not sure how to proceed.

I was expecting the ID attribute to remain the same regardless of how the site is accessed.

答案1

得分: 1

有些框架使用动态id来保护输入免受解析器的影响。我通过使用完整xpath进行元素搜索来解决了这个问题。

例子:

# xpath: 无法处理动态id
input = driver.find_element(By.XPATH, "//[@id='input-147']") 

# 完整xpath:适用于动态id
input = driver.find_element(By.XPATH, "/html/body/header/div/form/div/input")

通过xpath定位文档

英文:

Some frameworks use dynamic id for input to protect against parsers. I solved this problem using element search by full xpath.

Example:

# xpath: does not work with dynamic id
input = driver.find_element(By.XPATH, "//*[@id='input-147']") 

# full xpath: work with dynamic id
input = driver.find_element(By.XPATH, "/html/body/header/div/form/div/input")

Locating by xpath documentation

huangapple
  • 本文由 发表于 2023年2月9日 03:44:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390998.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定