现在我想使用selenium webdriver从网站上获取数据。现在我可以登录并转到主页面。有一个表,我想点击col“名字”下的每个名字(),这样我就可以去每个客户的个人资料进行更深入的搜索。我想先用一个列表来抓住每个用户。这是我的代码:
WebElement table = driver.findElement(By.id("ember2185"));
List<WebElement> list = (List<WebElement>) table.findElement(By.className("ember-view vertical-item"));
for (WebElement w : list) {
System.out.println(w);
}
这是html inspect:
<vertical-collection id="ember2185" class="ember-view">
<div id="ember2236" class="ember-view vertical-item" style="min-height: 76px;"> <div id="ember2256" class="ember-view row-patient list-item"><div class="col-pic"><img id="ember2265" src="assets/images/default_profile-51d6b94248e500ae7f03bc28a8110e4d.png" class="ember-view"></div>
<div class="row" data-guid="fad3e8d7-fb46-48a0-9e31-fd5577c17bab">
<div class="col-name">
<div data-element="patient-first-name"><p class="p-link">Cindy <!----><!----></p></div>
<div data-element="patient-record-number" class="prn"><span class="p-666">PRN </span><span>CO711603</span></div>
</div>
<div class="col-name">
<div data-element="patient-last-name"><p class="p-link">DemoObstetrics</p></div>
</div>
<div class="col-small">
<div id="ember2274" data-element="patient-dob" class="ember-view"><div class="visible-xs visible-sm">01/14/1982</div>
<div class="hidden-xs hidden-sm">Jan 14, 1982</div>
</div>
<div data-element="patient-gender">Female</div>
</div>
<div class="col-contact">
<div data-element="patient-address" class="ellipses" title="312 S 67th St,, Schenectady, NY 12345">312 S 67th St,, Schenectady, NY 12345</div>
<div data-element="patient-phone-numbers" class="ellipses">
<span id="ember2283" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">M </span>
<span>(555) 555-5555</span></span>
<span id="ember2284" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">H </span>
<span>(555) 555-5555</span></span>
</div>
</div>
<div class="col-small text-right" data-element="patient-last-accessed">
<div>6:02 PM</div>
<div>04/23/18</div>
</div>
</div>
</div>
</div><div id="ember2237" class="ember-view vertical-item" style="min-height: 76px;"> <div id="ember2285" class="ember-view row-patient list-item"><div class="col-pic"><img id="ember2286" src="assets/images/default_profile-51d6b94248e500ae7f03bc28a8110e4d.png" class="ember-view"></div>
<div class="row" data-guid="dd2308fa-01cb-4bc2-a0f1-abc08ff792c9">
<div class="col-name">
<div data-element="patient-first-name"><p class="p-link">Michelle <!----><!----></p></div>
<div data-element="patient-record-number" class="prn"><span class="p-666">PRN </span><span>MG875244</span></div>
</div>
<div class="col-name">
<div data-element="patient-last-name"><p class="p-link">DemoGyn</p></div>
</div>
<div class="col-small">
<div id="ember2287" data-element="patient-dob" class="ember-view"><div class="visible-xs visible-sm">05/16/1955</div>
<div class="hidden-xs hidden-sm">May 16, 1955</div>
</div>
<div data-element="patient-gender">Female</div>
</div>
<div class="col-contact">
<div data-element="patient-address" class="ellipses" title="123 S. 45th St., Anytown, NY 12345">123 S. 45th St., Anytown, NY 12345</div>
<div data-element="patient-phone-numbers" class="ellipses">
<span id="ember2288" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">M </span>
<span>(555) 555-5555</span></span>
<span id="ember2289" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">H </span>
<span>(555) 555-5555</span></span>
</div>
</div>
<div class="col-small text-right" data-element="patient-last-accessed">
<div>1:32 PM</div>
<div>04/18/18</div>
</div>
</div>
</div>
</div><div id="ember2238" class="ember-view vertical-item" style="min-height: 76px;"> <div id="ember2290" class="ember-view row-patient list-item"><div class="col-pic"><img id="ember2291" src="assets/images/default_profile-51d6b94248e500ae7f03bc28a8110e4d.png" class="ember-view"></div>
<div class="row" data-guid="477f5a0d-04e6-410b-afc6-adbacb55af99">
<div class="col-name">
<div data-element="patient-first-name"><p class="p-link">Eric <!----><!----></p></div>
<div data-element="patient-record-number" class="prn"><span class="p-666">PRN </span><span>EG629610</span></div>
</div>
<div class="col-name">
<div data-element="patient-last-name"><p class="p-link">DemoGastro</p></div>
</div>
<div class="col-small">
<div id="ember2292" data-element="patient-dob" class="ember-view"><div class="visible-xs visible-sm">05/12/1951</div>
<div class="hidden-xs hidden-sm">May 12, 1951</div>
</div>
<div data-element="patient-gender">Male</div>
</div>
<div class="col-contact">
<div data-element="patient-address" class="ellipses" title="3278 Maple St., San Antonio, TX 78023">3278 Maple St., San Antonio, TX 78023</div>
<div data-element="patient-phone-numbers" class="ellipses">
<span id="ember2293" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">M </span>
<span>(555) 555-5555</span></span>
<span id="ember2294" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">H </span>
<span>(555) 555-5555</span></span>
</div>
</div>
<div class="col-small text-right" data-element="patient-last-accessed">
<div>1:30 PM</div>
<div>04/18/18</div>
</div>
</div>
</div>
</div><div id="ember2239" class="ember-view vertical-item" style="min-height: 76px;"> <div id="ember2295" class="ember-view row-patient list-item"><div class="col-pic"><img id="ember2296" src="assets/images/default_profile-51d6b94248e500ae7f03bc28a8110e4d.png" class="ember-view"></div>
<div class="row" data-guid="e6bd1691-db04-4bfc-be72-1acc590cb93e">
<div class="col-name">
<div data-element="patient-first-name"><p class="p-link">Jennifer <!----><!----></p></div>
<div data-element="patient-record-number" class="prn"><span class="p-666">PRN </span><span>JR572205</span></div>
</div>
<div class="col-name">
<div data-element="patient-last-name"><p class="p-link">DemoRheum</p></div>
</div>
<div class="col-small">
<div id="ember2297" data-element="patient-dob" class="ember-view"><div class="visible-xs visible-sm">03/05/1970</div>
<div class="hidden-xs hidden-sm">Mar 05, 1970</div>
</div>
<div data-element="patient-gender">Female</div>
</div>
<div class="col-contact">
<div data-element="patient-address" class="ellipses" title="45 Deer Run Road, Livingston, NJ 07039">45 Deer Run Road, Livingston, NJ 07039</div>
<div data-element="patient-phone-numbers" class="ellipses">
<span id="ember2298" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">M </span>
<span>(555) 555-5555</span></span>
<span id="ember2299" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">H </span>
<span>(555) 555-5555</span></span>
</div>
</div>
<div class="col-small text-right" data-element="patient-last-accessed">
<div>10:51 PM</div>
<div>04/17/18</div>
</div>
</div>
</div>
</div><div id="ember2240" class="ember-view vertical-item" style="min-height: 76px;"> <div id="ember2300" class="ember-view row-patient list-item"><div class="col-pic"><img id="ember2301" src="assets/images/default_profile-51d6b94248e500ae7f03bc28a8110e4d.png" class="ember-view"></div>
<div class="row" data-guid="a6547f58-1af7-404d-8896-e95cd8ec2bd1">
<div class="col-name">
<div data-element="patient-first-name"><p class="p-link">Michael T<!----></p></div>
<div data-element="patient-record-number" class="prn"><span class="p-666">PRN </span><span>MO686770</span></div>
</div>
<div class="col-name">
<div data-element="patient-last-name"><p class="p-link">DemoPrimaryCare</p></div>
</div>
<div class="col-small">
<div id="ember2302" data-element="patient-dob" class="ember-view"><div class="visible-xs visible-sm">07/05/1958</div>
<div class="hidden-xs hidden-sm">Jul 05, 1958</div>
</div>
<div data-element="patient-gender">Male</div>
</div>
<div class="col-contact">
<div data-element="patient-address" class="ellipses" title="5432 Rebrow St, Lakeside, NY 27511">5432 Rebrow St, Lakeside, NY 27511</div>
<div data-element="patient-phone-numbers" class="ellipses">
<span id="ember2303" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">M </span>
<span>(555) 555-5555</span></span>
<span id="ember2304" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">H </span>
<span>(555) 555-5555</span></span>
</div>
</div>
<div class="col-small text-right" data-element="patient-last-accessed">
<div>10:51 PM</div>
<div>04/17/18</div>
</div>
</div>
</div>
</div><div id="ember2241" class="ember-view vertical-item" style="min-height: 76px;"> <div id="ember2305" class="ember-view row-patient list-item"><div class="col-pic"><img id="ember2306" src="assets/images/default_profile-51d6b94248e500ae7f03bc28a8110e4d.png" class="ember-view"></div>
<div class="row" data-guid="ce433b32-0ff7-4e42-b569-b1e7b8607524">
<div class="col-name">
<div data-element="patient-first-name"><p class="p-link">Eric R<!----></p></div>
<div data-element="patient-record-number" class="prn"><span class="p-666">PRN </span><span>EP108956</span></div>
</div>
<div class="col-name">
<div data-element="patient-last-name"><p class="p-link">DemoPsych</p></div>
</div>
<div class="col-small">
<div id="ember2307" data-element="patient-dob" class="ember-view"><div class="visible-xs visible-sm">03/01/1965</div>
<div class="hidden-xs hidden-sm">Mar 01, 1965</div>
</div>
<div data-element="patient-gender">Male</div>
</div>
<div class="col-contact">
<div data-element="patient-address" class="ellipses" title="783 Washington Ave, Hollytown, NH 33220">783 Washington Ave, Hollytown, NH 33220</div>
<div data-element="patient-phone-numbers" class="ellipses">
<span id="ember2308" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">M </span>
<span>(555) 555-5555</span></span>
<span id="ember2309" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">H </span>
<span>(555) 555-5555</span></span>
</div>
</div>
<div class="col-small text-right" data-element="patient-last-accessed">
<div>10:51 PM</div>
<div>04/17/18</div>
</div>
</div>
</div>
</div><div id="ember2242" class="ember-view vertical-item" style="min-height: 76px;"> <div id="ember2310" class="ember-view row-patient list-item"><div class="col-pic"><img id="ember2311" src="assets/images/default_profile-51d6b94248e500ae7f03bc28a8110e4d.png" class="ember-view"></div>
<div class="row" data-guid="4c54bc81-a6df-4409-9312-aef3ba6ed2ed">
<div class="col-name">
<div data-element="patient-first-name"><p class="p-link">Steve <!----><!----></p></div>
<div data-element="patient-record-number" class="prn"><span class="p-666">PRN </span><span>SS892268</span></div>
</div>
<div class="col-name">
<div data-element="patient-last-name"><p class="p-link">DemoCardio</p></div>
</div>
<div class="col-small">
<div id="ember2312" data-element="patient-dob" class="ember-view"><div class="visible-xs visible-sm">03/15/1943</div>
<div class="hidden-xs hidden-sm">Mar 15, 1943</div>
</div>
<div data-element="patient-gender">Male</div>
</div>
<div class="col-contact">
<div data-element="patient-address" class="ellipses" title="47 Peach Lane, Atlanta, GA 30032">47 Peach Lane, Atlanta, GA 30032</div>
<div data-element="patient-phone-numbers" class="ellipses">
<span id="ember2313" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">M </span>
<span>(555) 555-5555</span></span>
<span id="ember2314" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">H </span>
<span>(555) 555-5555</span></span>
</div>
</div>
<div class="col-small text-right" data-element="patient-last-accessed">
<div>10:51 PM</div>
<div>04/17/18</div>
</div>
</div>
</div>
</div><div id="ember2243" class="ember-view vertical-item" style="min-height: 76px;"> <div id="ember2315" class="ember-view row-patient list-item"><div class="col-pic"><img id="ember2316" src="assets/images/default_profile-51d6b94248e500ae7f03bc28a8110e4d.png" class="ember-view"></div>
<div class="row" data-guid="327fca40-c7b9-40af-ac77-83d6d8989372">
<div class="col-name">
<div data-element="patient-first-name"><p class="p-link">Nancy <!----><!----></p></div>
<div data-element="patient-record-number" class="prn"><span class="p-666">PRN </span><span>NO731672</span></div>
</div>
<div class="col-name">
<div data-element="patient-last-name"><p class="p-link">DemoObstetrics</p></div>
</div>
<div class="col-small">
<div id="ember2317" data-element="patient-dob" class="ember-view"><div class="visible-xs visible-sm">04/08/1982</div>
<div class="hidden-xs hidden-sm">Apr 08, 1982</div>
</div>
<div data-element="patient-gender">Female</div>
</div>
<div class="col-contact">
<div data-element="patient-address" class="ellipses" title="321 S. 54th St, Anytown, NY 12345">321 S. 54th St, Anytown, NY 12345</div>
<div data-element="patient-phone-numbers" class="ellipses">
<span id="ember2318" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">M </span>
<span>(555) 555-5555</span></span>
<span id="ember2319" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">H </span>
<span>(555) 555-5555</span></span>
</div>
</div>
<div class="col-small text-right" data-element="patient-last-accessed">
<div>10:51 PM</div>
<div>04/17/18</div>
</div>
</div>
</div>
</div><div id="ember2244" class="ember-view vertical-item" style="min-height: 76px;"> <div id="ember2320" class="ember-view row-patient list-item"><div class="col-pic"><img id="ember2321" src="assets/images/default_profile-51d6b94248e500ae7f03bc28a8110e4d.png" class="ember-view"></div>
<div class="row" data-guid="04cf0c5b-805b-4478-a6a9-744b9e4d1c18">
<div class="col-name">
<div data-element="patient-first-name"><p class="p-link">Brenda <!----><!----></p></div>
<div data-element="patient-record-number" class="prn"><span class="p-666">PRN </span><span>BO958186</span></div>
</div>
<div class="col-name">
<div data-element="patient-last-name"><p class="p-link">DemoOncology</p></div>
</div>
<div class="col-small">
<div id="ember2322" data-element="patient-dob" class="ember-view"><div class="visible-xs visible-sm">07/15/1951</div>
<div class="hidden-xs hidden-sm">Jul 15, 1951</div>
</div>
<div data-element="patient-gender">Female</div>
</div>
<div class="col-contact">
<div data-element="patient-address" class="ellipses" title="4700 Milword Drive, Stony Brook, NY 11790">4700 Milword Drive, Stony Brook, NY 11790</div>
<div data-element="patient-phone-numbers" class="ellipses">
<span id="ember2323" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">M </span>
<span>(555) 555-5555</span></span>
<span id="ember2324" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">H </span>
<span>(555) 555-5555</span></span>
</div>
</div>
<div class="col-small text-right" data-element="patient-last-accessed">
<div>10:51 PM</div>
<div>04/17/18</div>
</div>
</div>
</div>
</div><div id="ember2245" class="ember-view vertical-item" style="min-height: 76px;"> <div id="ember2325" class="ember-view row-patient list-item"><div class="col-pic"><img id="ember2326" src="assets/images/default_profile-51d6b94248e500ae7f03bc28a8110e4d.png" class="ember-view"></div>
<div class="row" data-guid="a221f6e9-dacf-4b1c-86cc-2712be3231fd">
<div class="col-name">
<div data-element="patient-first-name"><p class="p-link">John <!----><!----></p></div>
<div data-element="patient-record-number" class="prn"><span class="p-666">PRN </span><span>DP278077</span></div>
</div>
<div class="col-name">
<div data-element="patient-last-name"><p class="p-link">DemoPrimaryCare</p></div>
</div>
<div class="col-small">
<div id="ember2327" data-element="patient-dob" class="ember-view"><div class="visible-xs visible-sm">03/30/1977</div>
<div class="hidden-xs hidden-sm">Mar 30, 1977</div>
</div>
<div data-element="patient-gender">Male</div>
</div>
<div class="col-contact">
<div data-element="patient-address" class="ellipses" title="4994 Shady Range, Ossawinamakee Beach, MS 39469">4994 Shady Range, Ossawinamakee Beach, MS 39469</div>
<div data-element="patient-phone-numbers" class="ellipses">
<span id="ember2328" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">M </span>
<span>(555) 555-5555</span></span>
<span id="ember2329" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">H </span>
<span>(555) 555-5555</span></span>
</div>
</div>
<div class="col-small text-right" data-element="patient-last-accessed">
<div>10:51 PM</div>
<div>04/17/18</div>
</div>
</div>
</div>
</div><div id="ember2246" class="ember-view vertical-item" style="min-height: 76px;"> <div id="ember2330" class="ember-view row-patient list-item"><div class="col-pic"><img id="ember2331" src="assets/images/default_profile-51d6b94248e500ae7f03bc28a8110e4d.png" class="ember-view"></div>
<div class="row" data-guid="64cd540e-2482-49dc-9b68-952ff38e995d">
<div class="col-name">
<div data-element="patient-first-name"><p class="p-link">Jaclyn M<!----></p></div>
<div data-element="patient-record-number" class="prn"><span class="p-666">PRN </span><span>JE742116</span></div>
</div>
<div class="col-name">
<div data-element="patient-last-name"><p class="p-link">DemoENT</p></div>
</div>
<div class="col-small">
<div id="ember2332" data-element="patient-dob" class="ember-view"><div class="visible-xs visible-sm">10/21/1985</div>
<div class="hidden-xs hidden-sm">Oct 21, 1985</div>
</div>
<div data-element="patient-gender">Female</div>
</div>
<div class="col-contact">
<div data-element="patient-address" class="ellipses" title="323 Elast Drive, Brownsville, PA 49300">323 Elast Drive, Brownsville, PA 49300</div>
<div data-element="patient-phone-numbers" class="ellipses">
<span id="ember2333" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">M </span>
<span>(555) 555-5555</span></span>
<span id="ember2334" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">H </span>
<span>(555) 555-5555</span></span>
</div>
</div>
<div class="col-small text-right" data-element="patient-last-accessed">
<div>10:51 PM</div>
<div>04/17/18</div>
</div>
</div>
</div>
</div><div id="ember2247" class="ember-view vertical-item" style="min-height: 76px;"> <div id="ember2335" class="ember-view row-patient list-item"><div class="col-pic"><img id="ember2336" src="assets/images/default_profile-51d6b94248e500ae7f03bc28a8110e4d.png" class="ember-view"></div>
<div class="row" data-guid="f7ab7035-26f3-44d5-9b24-a44c8e8b62e9">
<div class="col-name">
<div data-element="patient-first-name"><p class="p-link">Shana <!----><!----></p></div>
<div data-element="patient-record-number" class="prn"><span class="p-666">PRN </span><span>SP708107</span></div>
</div>
<div class="col-name">
<div data-element="patient-last-name"><p class="p-link">DemoPeds</p></div>
</div>
<div class="col-small">
<div id="ember2337" data-element="patient-dob" class="ember-view"><div class="visible-xs visible-sm">11/08/2005</div>
<div class="hidden-xs hidden-sm">Nov 08, 2005</div>
</div>
<div data-element="patient-gender">Female</div>
</div>
<div class="col-contact">
<div data-element="patient-address" class="ellipses" title="488 Taylor St, San Francisco, CA">488 Taylor St, San Francisco, CA</div>
<div data-element="patient-phone-numbers" class="ellipses">
<span id="ember2338" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">M </span>
<span>(555) 555-5555</span></span>
<span id="ember2339" title="(555) 555-5555" class="ember-view phone-number"><span class="p-666">H </span>
<span>(555) 555-5555</span></span>
</div>
</div>
<div class="col-small text-right" data-element="patient-last-accessed">
<div>10:51 PM</div>
<div>04/17/18</div>
</div>
</div>
</div>
</div></vertical-collection>
似乎很长但我发现'id =“ember2185”'包含表中的所有元素,并且对于每一行,它们都具有类名“ember-view vertical-item”,这就是我使用类名的原因抓住所有的行。但是,它不起作用,错误消息是
"Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: Compound class names not permitted
(Session info: chrome=65.0.3325.181)
(Driver info: chromedriver=2.38.551581 (2c9c29527ada10af4745ab26dd000ebb6d5e055e),platform=Mac OS X 10.11.6 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:33:15.31Z'
System info: host: 'JiandeMacBook-Pro.local', ip: '2601:547:900:384f:0:0:0:6b7c', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.11.6', java.version: '1.8.0_111'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.38.551581 (2c9c29527ada10..., userDataDir: /var/folders/l1/4syvs2451pq...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 65.0.3325.181, webStorageEnabled: true}
Session ID: a9aaa265d0cf9f9e96d5072cfbfca7ee"
那有什么不对?如何单击表格中的每一行?谢谢你的帮助!
答案 0 :(得分:0)
如果要获取名字,请使用以下xpath选择器进行检查。
String firstName = driver.findElement(By.xpath(".//vertical-collection[@class='ember-view']/div[1]//*[@data-element='patient-first-name']")).getText();
选择器中指定的div[1]
将更改,现在在上面的xpath中,它将采用第一个数据的第一个名称。我认为它的辛迪。
因此,您可以使用循环迭代来获取所有值。
获取表格和名字总计数的示例代码如下所示,
int number = driver.findElements(By.xpath(".//vertical-collection[@class='ember-view']/div//*[@data-element='patient-first-name']")).size();
System.out.println("Count is : "+number);
for(int i=1;i<number;i++){
String firstName = driver.findElement(By.xpath(".//vertical-collection[@class='ember-view']/div["+i+"]//*[@data-element='patient-first-name']")).getText();
System.out.println("First name : "+ firstName);
}
输出:
Count is : 12
First name : Cindy
First name : Michelle
First name : Eric
First name : Jennifer
First name : Michael T
First name : Eric R
First name : Steve
First name : Nancy
First name : Brenda
First name : John
First name : Jaclyn M