如何在Selenium中使用Python语法通过XPath定位

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

How to syntax Locating by XPath in Selenium with Python

问题

我有困难访问此长路径中的文本字段,其中包含帧、表、tr和td。 请查看截图底部的行。
在这种情况下,有人可以给出语法应该是什么的提示吗?
有没有比在Chrome中检查更好的方法?

我必须找到一个字段并在其中插入文本。

英文:

i am having trouble to access a text field in this long path of frames, tabels, tr, td. Please see bottom line of screenshot.
Can someone give a hint on how the syntax in this case should be?
Is there a better way of doing this rathen then inspect in chrome?

I have to find a field and insert text into it.

Screenshot

答案1

得分: 1

尝试以下代码来定位所需的元素并将文本发送到其中:

# 显式等待10秒后获取元素
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='IX_SEARCH_VALUE_2']")))
# 在这里输入文本
element.send_keys("在这里输入文本")

您需要以下导入语句:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

我在您提供的截图中没有看到任何 iframe。如果存在 iframe,首先切换到包含该元素的 iframe,然后执行操作。

切换到 iframe 的示例代码:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@id='iframe']"))

这些是您提供的代码段的翻译部分。

英文:

Try the below code to locate the desired element and to send text into it:

# get element  after explicitly waiting for 10 seconds
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='IX_SEARCH_VALUE_2']")))
# input the text here
element.send_keys("enter the text here")

You will need below import statement:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

I do not see any iframe in the screenshot you have provided. If in case an iframe exist, first switch to the iframe within which the element is located, and then perform action.

Example code to switch into an iframe:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@id='iframe']")))

答案2

得分: 0

要使用Selenium通过XPATH查找元素,请使用find_element

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("google.com") # 替换为您的URL

my_element = driver.find_element(By.XPATH, "//input[@name='IX_SEARCH_VALUE_2']")

print(my_element.text)
英文:

To find element by XPATH with selenium, use find_element

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("google.com") #replace with your url

my_element = driver.find_element(By.XPATH, "//input[@name='IX_SEARCH_VALUE_2']")

print(my_element.text)

答案3

得分: 0

CSS选择器是 input[name='IX_SEARCH_VALUE_2']

英文:

The css is input[name='IX_SEARCH_VALUE_2']

答案4

得分: 0

iframe = driver.find_element(By.XPATH, "/html/frameset/frameset/frameset/frame[3]")
driver.switch_to.frame(iframe)

I had to switch to the frame at first.
by the way, selectorshub is a very nice plugin. On top of the inspector in chrome

英文:
iframe = driver.find_element(By.XPATH, "/html/frameset/frameset/frameset/frame[3]")
driver.switch_to.frame(iframe)

I had to switch to the frame at first.
by the way, selectorshub is a very nice plugin. On top of the inspector in chrome

huangapple
  • 本文由 发表于 2023年3月9日 23:00:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686342.html
匿名

发表评论

匿名网友

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

确定