尝试通过网络驱动程序填充文本框

问题描述 投票:0回答:1

尝试通过网络驱动程序将文本发送到文本框,

driver.find_element(By.ID, 'input-description-2').send_keys(description) 

不起作用,因为它说找不到 id ,

我需要将(描述)变量发送到下面的文本框,这是带有 aria-label 的市场片段,下面包含单词“test”的文本是它需要发送到的地方, 解决这个问题的最佳方法是什么?

<body aria-label="Editor, input-description-2" role="textbox" aria-multiline="true" contenteditable="true" aria-readonly="false" class="cke_editable cke_editable_themed cke_contents_ltr cke_show_borders" spellcheck="true"><p>test<br></p></body>
driver.find_element(By.ID).send_keys(description) 

我尝试通过上面的方法进行操作,但我可能需要以不同的方式指定它,因为它使用了 aria-label?

python selenium-webdriver selenium-chromedriver webdriver
1个回答
0
投票

您可以尝试使用 XPATH

from selenium.webdriver.common.by import By

# Using XPath to find the element by aria-label attribute
element = driver.find_element(By.XPATH, '//body[@aria-label="Editor, input-description-2"]')

# Sending keys to the element
element.send_keys(description)

您也可以尝试使用 CSS_SELECTOR

 from selenium.webdriver.common.by import By

# Using CSS selector to find the element by aria-label attribute
element = driver.find_element(By.CSS_SELECTOR, 'body[aria-label="Editor, input-description-2"]')

# Sending keys to the element
element.send_keys(description)
© www.soinside.com 2019 - 2024. All rights reserved.