我想在硒中找到我的TextField,但我不知道该怎么做(我第一次使用硒)。
我尝试了以下选项来尝试,但是没有用
driver.find_element_by_name(Token).send_keys(abcdefghijklm)
driver.find_element_by_class_name('form-control.ng-untouched.ng-pristine.ng-valid').send_keys("asbfsbgkf")
driver.find_element_by_cssSelector("input.form-control ng-untouched ng-pristine ng-valid[placeholder='Token string']")
driver.findElementByCSSSelector("input.form-control ng-untouched ng-pristine ng-valid[placeholder='Token string']")
driver.find_element_by_xpath("//input[@class='form-control ng-untouched ng-pristine ng-valid'][@type='text']").send_keys("asbfsbgkf")
有人,请在这里帮助我。我真的很感激。
以下是HTML :(突出显示了我需要搜索的内容-“第二值”输入)
`
<html lang="en" class="gr__xx_xx_xx_xx_xx_xx_xx">
<body data-gr-c-s-loaded="true"><!-- <g:savetophone id="savetomobile_tag" --><!-- onsuccess="successHandler"--><!--onfailure="failureHandler" size="small" theme="light" ></g:savetomobile>--><app-root _nghost-c0="" ng-version="4.4.6"><div _ngcontent-c0="" class="container-fluid">
<nav _ngcontent-c0="" class="navbar navbar-expand-lg navbar-light bg-light">
<button _ngcontent-c0="" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" class="navbar-toggler" data-target="#navbarSupportedContent" data-toggle="collapse" type="button">
<span _ngcontent-c0="" class="navbar-toggler-icon"></span>
</button>
<div _ngcontent-c0="" class="collapse navbar-collapse" id="navbarSupportedContent">
<ul _ngcontent-c0="" class="navbar-nav mr-auto">
<li _ngcontent-c0="" class="nav-item active">
POC Tester
</li>
</ul>
</div>
</nav>
<div _ngcontent-c0="" class="row">
<div _ngcontent-c0="" class="col-12">
<app-user-input _ngcontent-c0=""><div>
<div class="jumbotron">
<h3>Please provide the following information</h3>
<hr class="my-4">
<div>
</div>
<div class="input-group-mt-2">
<span class="input-group-btn">
<div class="d-inline-block dropdown" ngbdropdown="">
<button aria-haspopup="true" class="btn btn-outline-primary dropdown-toggle" id="dropdownBasic1" ngbdropdowntoggle="" aria-expanded="false">environment</button>
<div aria-labelledby="dropdownBasic1" ngbdropdownmenu="" class="dropdown-menu">
<!----><button class="dropdown-item">status</button><button class="dropdown-item">status</button>
</div>
</div>
</span>
<input aria-describedby="basic-addon1" aria-label="URL" class="form-control ng-untouched ng-pristine ng-valid" placeholder="https://something.something.com" type="text">
</div>
<div class="input-group-mt-2">
<span class="input-group-btn">
<div class="d-inline-block dropdown" ngbdropdown="">
<button aria-haspopup="true" class="btn btn-outline-primary dropdown-toggle" id="dropdownBasic1" ngbdropdowntoggle="" aria-expanded="false">Enjoy button</button>
<div aria-labelledby="dropdownBasic1" ngbdropdownmenu="" class="dropdown-menu">
<!----><button class="dropdown-item">Enjoy button</button><button class="dropdown-item">Offers & Notifications</button>
</div>
</div>
</span>
<input aria-describedby="basic-addon1" aria-label="URL" class="form-control ng-untouched ng-pristine ng-valid" placeholder="https://something.somewhere.com" type="text">
</div>
<div class="input-group mt-2">
<span class="input-group-btn">
<button class="btn btn-secondary" type="button">First value</button>
</span>
<input aria-describedby="basic-addon1" aria-label="URL" class="form-control ng-untouched ng-pristine ng-valid" placeholder="test value" type="text">
</div>
<div class="input-group mt-2">
<span class="input-group-btn">
<button class="btn btn-secondary" type="button">Second Value</button>
</span>
**<input aria-describedby="basic-addon1" aria-label="URL" class="form-control ng-untouched ng-pristine ng-valid" placeholder="user value" type="text">**
</div>
<div class="input-group mt-2">
<span class="input-group-btn">
<button class="btn btn-secondary" type="button">third value</button>
</span>
<input aria-describedby="basic-addon1" aria-label="URL" class="form-control ng-untouched ng-pristine ng-valid" placeholder="test user" type="text">
</div>
<div class="mt-2">
<button class="btn btn-primary">Generate value</button>
</div>
<!----><div class="mt-4">
press:
<div id="location">
</div>
</div>
<div class="mt-2">
<span></span>
</div>
<ngb-progressbar type="success">
<div class="progress">
<div aria-valuemin="0" role="progress" class="progress bg-success" aria-valuenow="0" aria-valuemax="100" style="width: 0%;">
<!---->
</div>
</div>
</ngb-progressbar>
</div>
<div class="mt-5">
<p>content:</p>
<textarea class="form-control ng-untouched ng-pristine ng-valid" rows="20"></textarea>
</div>
</div>
</app-user-input>
</div>
</div>
</div></app-root><script type="text/javascript" src="inline.0c00000000000000ca85.bundle.js"></script><script type="text/javascript" src="000000000000000015b.bundle.js"></script><script type="text/javascript" src="scripts.0000000000000fc0000.bundle.js"></script><script type="text/javascript" src="vendor.00000000fgf000000gdf.bundle.js"></script><script type="text/javascript" src="main.cf3000000000000eb000a.bundle.js"></script></body></html>
答案 0 :(得分:0)
在大多数情况下,您的xpaths返回列表。既然我们知道我们正在寻找第二个输入元素,我们可以使用以下代码从列表中获取第二个结果:
second_input_xpath = "//div[@class='input-group-mt-2'][2]//input"
driver.find_element_by_xpath(second_input_xpath ).send_keys("asbfsbgkf")
关于标题中的主要错误,似乎您没有正确初始化Chrome网络驱动程序。我建议您查看here的硒文档。
答案 1 :(得分:0)
看起来所有输入字段都具有相同的类名。因此,请使其具有唯一性。
driver.find_element_by_xpath("(//input[@class='form-control ng-untouched ng-pristine ng-valid'])[n]").send_keys("value")
where n=1,2,3...n
第一个元素的PS n = 1。请告诉我这是否有帮助。