英文:
No element found by XPATH
问题
我正在尝试使用以下XPATH检索表中特定元素的文本:
driver.maximize_window() # 用于最大化窗口
driver.implicitly_wait(3) # 提供了一个20秒的隐式等待
driver.find_element(By.XPATH, value="/html/body/form[2]/table/tbody/tr/td/table/tbody/tr[2]/td[7]/input").text
但是我收到以下错误消息:
NoSuchElementException: Message: 无法找到元素:/html/body/form[2]/table/tbody/tr/td/table/tbody/tr[2]/td[7]/input
我还尝试通过CSS选择器和值来访问元素,但没有成功。不幸的是,链接受到了保护,所以我无法分享它,但这里是该元素的屏幕截图。
英文:
I am trying to retrieve the text of a specific element in a table with the following XPATH:
/html/body/form[2]/table/tbody/tr/td/table/tbody/tr[2]/td[7]/input
using
driver.maximize_window() # For maximizing window
driver.implicitly_wait(3) # gives an implicit wait for 20 seconds
driver.find_element(By.XPATH, value = "/html/body/form[2]/table/tbody/tr/td/table/tbody/tr[2]/td[7]/input").text()
but I get the following error:
NoSuchElementException: Message: Unable to locate element: /html/body/form[2]/table/tbody/tr/td/table/tbody/tr[2]/td[7]/input
I have also tried accessing the element by CSS selector and value, without success.
Unfortunately the link is secured so I cannot share it but here is a screenshot of the element
答案1
得分: 5
尝试相对 XPath,而不是绝对 XPath。以下是表达式:
//input[@name='AdjIncrementAmount_1']
从截图上看,我不太确定属性 name
(在文本 Amount
之后)的值是否有一个 _
还是两个 __
。如果上面的(一个 _
)不起作用,请尝试下面的带有 2 个下划线:
//input[@name='AdjIncrementAmount__1']
英文:
Instead of your absolute XPath, try relative XPath. Below is the expression:
//input[@name='AdjIncrementAmount_1']
from the screenshot, I am not really sure if the value of attribute name
(after the text Amount
) has one _
or two __
. If the above with(one _
) doesn't work, try with 2 as below:
//input[@name='AdjIncrementAmount__1']
答案2
得分: 1
- 使用 css_selector:
print(driver.find_element(By.CSS_SELECTOR, "td > input[name='AdjIncrementAmount__1']").get_attribute("value"))
- 使用 xpath:
print(driver.find_element(By.XPATH, "//td/input[@name='AdjIncrementAmount__1']").get_attribute("value"))
- 注意:你需要添加以下导入语句:
from selenium.webdriver.common.by import By
英文:
To print the text $36,400.00 you can use either of the following locator strategies:
-
Using css_selector:
print(driver.find_element(By.CSS_SELECTOR, "td > input[name='AdjIncrementAmount__1']").get_attribute("value"))
-
Using xpath:
print(driver.find_element(By.XPATH, "//td/input[@name='AdjIncrementAmount__1']").get_attribute("value"))
-
Note : You have to add the following imports :
from selenium.webdriver.common.by import By
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论