英文:
Selenium "find_element_by_xpath" is gone. How to update variable that relied on that function?
问题
我看到我的问题的一部分已经在之前得到了答案:https://stackoverflow.com/questions/72754651/attributeerror-webdriver-object-has-no-attribute-find-element-by-xpath
我相信现在如果我想实现(例如):
driver.find_element_by_xpath('//*[@id="#ICClear"]').click()
我需要使用:
driver.find_element("xpath", '//*[@id="#ICClear"]').click()
然而,我在编程方面很不熟练,所以在我的代码中,我将我的一个脚本“定义”为:
xpath = driver.find_element_by_xpath
这样以后我可以使用:
xpath('''//*[@id="#ICClear"]''').click()
(请注意,我不仅仅使用click方法,还会发送文本等等。)
我有大约20个左右的脚本导入了这个“xpath”的定义,并在整个代码中使用它。我不确定如何更改我的“xpath”定义以适应新的格式,以便我仍然可以引用它而不必重构依赖于它的所有代码。
英文:
I saw that part of my question was answered before: https://stackoverflow.com/questions/72754651/attributeerror-webdriver-object-has-no-attribute-find-element-by-xpath
And I believe that now if I want to accomplish (for example):
driver.find_element_by_xpath('//*[@id="#ICClear"]').click()
I need to use:
driver.find_element("xpath", '//*[@id="#ICClear"]').click()
However, I'm very unsophisticated in programming, so in my code I, for better or worse, have one of my scripts "defining" this functionality as:
xpath = driver.find_element_by_xpath
So that later on I would use:
xpath("""//*[@id="#ICClear"]""").click()
(Note that I do more than just use the click method, I also send text, etc.)
I have about 20 or so scripts that import this definition of "xpath" and use it throughout. I'm not sure how to change my 'xpath' definition to work with the new format so that I can still reference it without refactoring all of the code that relies on this.
答案1
得分: 1
我还没有测试过这个,但我希望这样做可以满足你的要求:
def xpath(xpath_expr):
return driver.find_element("xpath", xpath_expr)
这将替代你在通用脚本中的 xpath
定义为 driver.find_element_by_xpath
。
英文:
I haven't tested this, but I would expect this to do what you are asking for:
def xpath(xpath_expr):
return driver.find_element("xpath", xpath_expr)
This would replace your definition of xpath
as driver.find_element_by_xpath
in your common script.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论