Python XPath 选择器不适用于 selenium
Python XPath selector not working with selenium
我尝试自动化一些输入。为此,我需要在
标签后输入一些文本。
为了确定输入的位置,我尝试了 XPath for fowlloing HTML 代码。
<span data-offset-key="1dq3m-0-0">
<br data-text="true">
</span>
这是我在 python 中写的内容。
buf_comp_text = 'foobar'
el_xp_comp_text = '//*[@data-text]'
...
## create post in queue (comment)
print('create post in queue - text')
post_txt = driver.find_element_by_xpath(el_xp_comp_text).send_keys(buf_comp_text)
不幸的是,我总是收到错误消息:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@data-text]"}
如有任何提示,我们将不胜感激。
您需要定位 <span>
标签,而不是定位 <br>
标签,您可以使用以下 :
使用css_selector
:
buf_comp_text = 'foobar'
driver.find_element_by_css_selector("span[data-offset-key]").send_keys(buf_comp_text)
使用xpath
:
buf_comp_text = 'foobar'
driver.find_element_by_xpath("//span[@data-offset-key][.//br[@data-text]]").send_keys(buf_comp_text)
理想情况下,要将 字符序列 发送到您需要为 element_to_be_clickable()
引入 WebDriverWait 的元素,您可以使用以下任一项 :
使用CSS_SELECTOR
:
buf_comp_text = 'foobar'
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span[data-offset-key]"))).send_keys(buf_comp_text)
使用XPATH
:
buf_comp_text = 'foobar'
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@data-offset-key][.//br[@data-text]]"))).send_keys(buf_comp_text)
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
参考
您可以在以下位置找到一些相关讨论:
- Switch to an iframe through Selenium and python
我尝试自动化一些输入。为此,我需要在
标签后输入一些文本。
为了确定输入的位置,我尝试了 XPath for fowlloing HTML 代码。
<span data-offset-key="1dq3m-0-0">
<br data-text="true">
</span>
这是我在 python 中写的内容。
buf_comp_text = 'foobar'
el_xp_comp_text = '//*[@data-text]'
...
## create post in queue (comment)
print('create post in queue - text')
post_txt = driver.find_element_by_xpath(el_xp_comp_text).send_keys(buf_comp_text)
不幸的是,我总是收到错误消息:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@data-text]"}
如有任何提示,我们将不胜感激。
您需要定位 <span>
标签,而不是定位 <br>
标签,您可以使用以下
使用
css_selector
:buf_comp_text = 'foobar' driver.find_element_by_css_selector("span[data-offset-key]").send_keys(buf_comp_text)
使用
xpath
:buf_comp_text = 'foobar' driver.find_element_by_xpath("//span[@data-offset-key][.//br[@data-text]]").send_keys(buf_comp_text)
理想情况下,要将 字符序列 发送到您需要为 element_to_be_clickable()
引入 WebDriverWait 的元素,您可以使用以下任一项
使用
CSS_SELECTOR
:buf_comp_text = 'foobar' WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span[data-offset-key]"))).send_keys(buf_comp_text)
使用
XPATH
:buf_comp_text = 'foobar' WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@data-offset-key][.//br[@data-text]]"))).send_keys(buf_comp_text)
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
参考
您可以在以下位置找到一些相关讨论:
- Switch to an iframe through Selenium and python