python - 如何使用 PhantomJS 在 Selenium Webdriver 中用文本填充 <p> 标记?

标签 python selenium webdriver phantomjs webautomation

我有一个输入表单,需要用文本填充。它是一个 div,有一个子节点,该子节点是一个

标记,需要填充文本才能提交表单。

我在 div 本身上尝试了 send_keys 但没有成功,在浏览器中我选择了

标签并更改了它的 TextContent 属性,这导致消息框被文本填充,所以我知道必须填写

标记,但在其上使用 send_keys 不起作用:

textbox = driver.find_elements_by_xpath(".//div[@role='textbox']/p[1]")[0]
print(textbox)
//<selenium.webdriver.remote.webelement.WebElement (session="a0712590-511d-11e6-8e12-dbe0d5eb709e", element=":wdc:1469309865349")>

现在使用 send_keys:

   textbox = driver.find_elements_by_xpath(".//div[@role='textbox']/p[1]")[0]
   textbox.send_keys("This is a test")
//selenium.common.exceptions.WebDriverException: Message: Error Message => 
''undefined' is not an object (evaluating 'a.value.length')'       

我的问题是,如何在该文本框中输入文本?

最佳答案

send_keys() 仅适用于需要在其 value 属性上设置值的元素,意味着 inputtextarea,但在这里您尝试在 p 元素上设置值,该元素需要在其 textContent 上设置,因此这里您应该尝试使用 execute_script() 如下:-

textbox = driver.find_element_by_xpath(".//div[@role='textbox']/p[1]")
driver.execute_script("arguments[0].textContent = arguments[1];", textbox, "This is a test")

或者

textbox = driver.find_elements_by_xpath(".//div[@role='textbox']/p[1]")[0]
driver.execute_script("arguments[0].textContent = arguments[1];", textbox, "This is a test")

希望有帮助...:)

关于python - 如何使用 PhantomJS 在 Selenium Webdriver 中用文本填充 <p> 标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38546693/

相关文章:

python - Matplotlib:子图中不同的x轴标签

python - 有条件地用不同 df 的值填充 pandas df 的列

ruby - $b.link( :text => "Sign up").when_present.click 在雅虎上不起作用

java - 运行 Cucumber 测试时出现异常

python - 使用 python selenium 在浏览器中创建警报窗口

java - 如何修复 "Exception in thread "main"java.lang.VerifyError : Cannot inherit from final class "error

java - Selenium - 无法使用 Actions 类通过几个步骤执行操作,有什么问题吗?

python - sublime text + python 的 anaconda

python - Simpy 3 : Resources. Resource.request()/.release() 没有 'with...as:'

javascript - 如果测试是用 Java 或 Javascript 编写的,selenium 端到端测试会运行得更快吗?