我正在尝试使用Selenium
清除Python3
的文本框(文本区域)。
我找到了文本框
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()
仍然没有清除这个元素:(任何想法非常感谢。
提前致谢。
感谢您的反馈,安迪
感谢大家抽出宝贵时间发表评论。
安迪我尝试过:
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和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)
我得到与Andy代码类似的错误:
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一词的语法仍然损坏。 '
谢谢
感谢您回覆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
**当我尝试不带WebElement字的Andy代码时**
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:// * [@@ =='scope']
查找文本框。
即使当我将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>
最佳答案
.clear()
方法适用于文本区域,但是如果您的情况下.clear()
不起作用,则可以执行java script
清除textarea
,如下所示:
myTextarea = mydriver.find_element_by_xpath("//*[@name='scope']")
mydriver.execute_script("arguments[0].value = '';", myTextarea)
希望它对您有用... :)
关于css - Python3 Selenium清除表格中的文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37916162/