英文:
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")
英文:
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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论