我正在尝试使用Selenium
使用Python3
清除文本框(textarea)。
我找到了
的文本框XPATH: //*[@name='scope']
我希望清除此元素中的文字。
以下是代码:
<table class="tdbglc" width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr class="tdbgdc">
<tr class="tdbglc">
<td class="bodytextgray" width="3%" valign="top" height="30" align="center">2.</td>
<td class="bodytextgray" width="47%" valign="top" height="30" align="left"> Network Scope (Up to 30 IP subnets, one on each line): </td>
<td class="bodytextgray" width="50%" height="30" colspan="2">
<textarea rows="4" cols="40" tabindex="2" name="scope">192.168.1.0/255.255.255.0</textarea>
*以下是我的尝试:*
mydriver.find_element_by_xpath("//*[@name='scope']").clear()
mydriver.find_elements_by_xpath("//*[contains(text(), '192.168.1.0/255.255.255.0')]").clear()
仍然没有清除这个元素:(任何想法非常赞赏。
先谢谢。
感谢您的反馈,Andy
感谢所有人抽出时间发表评论。 安迪我试过:
WebElement myTextarea = mydriver.find_element_by_xpath("//*[@name='scope']")
myTextarea.sendKeys(Keys.CONTROL + "a");
myTextarea.sendKeys(Keys.DELETE);
我现在收到此错误:
C:\Python\Python35\python.exe C:/Python/Python35/webtest4.py
File "C:/Python/Python35/webtest4.py", line 41
WebElement myTextarea = mydriver.find_element_by_xpath("//*[@name='scope']")
^
SyntaxError: invalid syntax
Process finished with exit code 1
道歉所有我是新手!
*我想我错过了某点:
Andy's和Saurabh的评论代码词:
WebElement
看起来会产生错误。
我试过了:
from selenium.webdriver.remote.webelement import WebElement
并且关键字在PyCharm中看起来仍然是错误。
添加后:
WebElement myTextarea = mydriver.find_element_by_xpath("//*[@name='scope']")
mydriver.execute_script("arguments[0].value = '';", myTextarea)
我得到了与安迪的代码类似的错误:
C:\Python\Python35\python.exe C:/Python/Python35/webtest4.py
File "C:/Python/Python35/webtest4.py", line 41
WebElement myTextarea = mydriver.find_element_by_xpath("//*[@name='scope']")
^
SyntaxError: invalid syntax
我觉得缺少某些东西,或者我的语法在Python 3中仍然用于WebElement这个词。 &#39;
由于
感谢Saurabh的回复。
当我只使用此代码时:
myTextarea = mydriver.find_element_by_xpath("//*[@name='scope']")
mydriver.execute_script("arguments[0].value = '';", myTextarea)
我收到此错误:
C:\Python\Python35\python.exe C:/Python/Python35/webtest4.py
Traceback (most recent call last):
File "C:/Python/Python35/webtest4.py", line 41, in <module>
myTextarea = mydriver.find_element_by_xpath("//*[@name='scope']")
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 293, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 745, in find_element
{'using': by, 'value': value})['value']
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//*[@name='scope']"}
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///C:/Users/gjones1/AppData/Local/Temp/tmpk3yo6e94/extensions/fxdriver@googlecode.com/components/driver-component.js:10770)
at fxdriver.Timer.prototype.setTimeout/<.notify (file:///C:/Users/gjones1/AppData/Local/Temp/tmpk3yo6e94/extensions/fxdriver@googlecode.com/components/driver-component.js:625)
Process finished with exit code 1
**当我尝试使用Andy的代码时没有使用WebElement **
这个词myTextarea = mydriver.find_element_by_xpath("//*[@name='scope']")
myTextarea.sendKeys(Keys.CONTROL + "a");
myTextarea.sendKeys(Keys.DELETE);
我收到此错误:
C:\Python\Python35\python.exe C:/Python/Python35/webtest4.py
Traceback (most recent call last):
File "C:/Python/Python35/webtest4.py", line 41, in <module>
myTextarea = mydriver.find_element_by_xpath("//*[@name='scope']")
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 293, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 745, in find_element
{'using': by, 'value': value})['value']
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//*[@name='scope']"}
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///C:/Users/gjones1/AppData/Local/Temp/tmp78badauy/extensions/fxdriver@googlecode.com/components/driver-component.js:10770)
at fxdriver.Timer.prototype.setTimeout/<.notify (file:///C:/Users/gjones1/AppData/Local/Temp/tmp78badauy/extensions/fxdriver@googlecode.com/components/driver-component.js:625)
Process finished with exit code 1
请注意我已经重新确认了XPATH:// * [@ name =&#39;范围&#39;] 找到文本框。
即使我将XPATH更改为其他找到文本框的内容,如:
//textarea[@tabindex='2']
//textarea[contains(@name,'scope')]
出现相同的错误。
到目前为止,谢谢。
以下是该网站的完整代码:
<html>
<head>
<frameset onload="frameLoad()" onbeforeunload="preUnloadPage()" onunload="pageUnload()" frameborder="NO" framespacing="0" border="0" rows="65,0,*">
<frame noresize="" frameborder="NO" marginwidth="0" marginheight="0" scrolling="no" src="mainEx.jsp" name="nav">
<frame noresize="" frameborder="NO" marginwidth="0" marginheight="0" scrolling="no" src="empty.html" name="statusFrm">
<frame noresize="" frameborder="0" scrolling="auto" src="blank.htm" name="ngbody">
<html>
<head>
<body bgcolor="#ffffff">
<div align="center">
<table width="100%" cellspacing="0" cellpadding="1" border="0">
<form action="/common/discovery.do" method="POST" name="discoveryForm">
<table class="tdbglc" width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr class="tdbgdc">
<tr class="tdbglc">
<td class="bodytextgray" width="3%" valign="top" height="30" align="center">2.</td>
<td class="bodytextgray" width="47%" valign="top" height="30" align="left"> Network Scope (Up to 30 IP subnets, one on each line): </td>
<td class="bodytextgray" width="50%" height="30" colspan="2">
<textarea rows="4" cols="40" tabindex="2" name="scope">192.168.1.0/255.255.255.0</textarea>
<img class="tooltip" src="../resource/skins/default/icons/help.png" tooltip="Discover devices that have IP addresses in the following subnets. At least one IP subnet in network/mask format (eg. <i>192.168.1.0/255.255.255.0</i> or <i>172.25.8.99/255.255.255.255</i>) must be specified, one on each row. When using the Seed Router option below, discovered devices not part of the specified subnets will be ignored (empty list will accept all)." style="float: right" data-hasqtip="1">
</td>
</tr>
<tr class="tdbgdc">
<tr class="tdbglc">
<tr class="tdbgdc">
</tbody>
</table>
<table id="advancedOptionsTable" class="tdbglc" width="100%" cellspacing="0" cellpadding="0" border="0">
<div id="advancedOptions" class="ui-helper-hidden">
<table class="tdbglc" width="100%" cellspacing="0" cellpadding="0" border="0">
<br>
<br>
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<input type="hidden" value="true" name="redrawn">
</form>
<script type="text/javascript" language="JavaScript">
</div>
<script type="text/javascript">
</body>
</html>
</frame>
</frameset>
</html>
答案 0 :(得分:0)
.clear()
方法适用于文字区域,但如果您的情况.clear()
无效,则可以执行java script
清除textarea
,如下所示: -
myTextarea = mydriver.find_element_by_xpath("//*[@name='scope']")
mydriver.execute_script("arguments[0].value = '';", myTextarea)
希望它对你有用...... :)