我想使用selenium打印一些数据,这里是HTML:
<div class="details-row section box-margin-LRmd is-disabled">
<div class="inline-flex-group-v2">
<div class="item gutter-sm box-fixed col-encounter-type">
<label class="field-label">Encounter type</label>
<div data-element="encounter-details-encounter-type">
Office Visit
<!----> </div>
</div>
<div class="item gutter-sm box-fixed col-extra-padding">
<label class="field-label">Note type</label>
<div data-element="encounter-details-encounter-type">Case Review</div>
</div>
<div class="item gutter-sm box-fixed col-date">
<label class="field-label">Date</label>
<div data-element="encounter-details-date" class="clearfix">
05/01/2018
</div>
</div>
<div class="item gutter-sm box-fixed col-age visible-lg">
<label class="field-label">Age at encounter</label>
<div data-element="encounter-details-age">60 yrs</div>
</div>
<div class="item gutter-sm box-fixed col-extra-padding visible-xl">
<label class="field-label">Seen by</label>
<div data-element="encounter-details-seen-by">Kristina Abernathy</div>
</div>
<div class="item gutter-sm box-fixed col-extra-padding visible-xl">
<label class="field-label">Facility</label>
<div data-element="encounter-details-facility">FOCUS Pittsburgh Free Health Center</div>
</div>
<div class="item gutter-sm visible-xl">
<label class="field-label">Status</label>
<div>
<span data-element="encounter-details-signed-by">Electronically signed by Kristina Abernathy at 05/01/2018 07:21 pm</span>
</div>
</div>
<!----> </div>
<div class="inline-flex-group-v2 hidden-xl box-padding-Tmd-v2 lower">
<div class="item gutter-lg box-fixed col-age hidden-lg">
<label class="field-label">Age at encounter</label>
<div data-element="encounter-details-age">60 yrs</div>
</div>
<div class="item gutter-lg box-fixed col-seen-by">
<label class="field-label">Seen by</label>
<div data-element="encounter-details-seen-by">Kristina Abernathy</div>
</div>
<div class="item gutter-lg box-fixed">
<label class="field-label">Facility</label>
<div data-element="encounter-details-facility">Health Center</div>
</div>
<div class="item gutter-sm">
<label class="field-label">Status</label>
<div>
<span data-element="encounter-details-signed-by">Electronically signed by Kristina Abernathy at 05/01/2018 07:21 pm</span>
</div>
</div>
<!----> </div>
</div>
我想打印遭遇类型,备注类型,日期,年龄和所见。所以我使用代码:
System.out.println("Encounter details");
System.out.println("ENCOUNTER TYPE: " + driver.findElement(By.xpath("//div[@data-element='encounter-details-encounter-type']")).getText());
System.out.println("NOTE TYPE: " + driver.findElement(By.xpath("//div[@data-element='encounter-details-encounter-type']")).getText());
System.out.println("DATE: " + driver.findElement(By.xpath("//div[@data-element='encounter-details-date']")).getText());
System.out.println("AGE OF ENCOUNTER: " + driver.findElement(By.xpath("//div[@data-element='encounter-details-age']")).getText());
System.out.println("SEEN BY: " + driver.findElement(By.xpath("//div[@data-element='encounter-details-seen-by']")).getText());
我得到了:
Encounter details
ENCOUNTER TYPE: Office Visit
NOTE TYPE: Office Visit
DATE: 05/01/2018
AGE OF ENCOUNTER: 60 yrs
SEEN BY:
所以遭遇类型和音符类型具有相同的属性“data-element ='encounter-details-encounter-type'”,这就是为什么我得到“遇到细节”和“遭遇类型”相同的结果,但我想要的是什么得到了
ENCOUNTER TYPE: Office Visit
NOTE TYPE: Case Review
DATE: 05/01/2018
AGE OF ENCOUNTER: 60 yrs
SEEN BY: Kristina Abernathy
那么我应该如何获得正确的音符类型以及为什么我不能获得“SEEN BY”内容?谢谢!
答案 0 :(得分:1)
从你的HTML我可以看到遭遇类型和注释类型字段使用相同的元素作为参考。这就是为什么你得到不同标题相同的结果。在选择用于标识元素的定位器时,最好为该元素使用唯一值,否则会出现这类问题。请使用以下代码查看注释类型,
System.out.println("NOTE TYPE: " + driver.findElement(By.xpath("//div[label='Note type']//*[@data-element='encounter-details-encounter-type']")).getText());
我检查了 SEEN BY 字段,它工作正常。
但如果您想拍摄第一张 SEEN BY 记录,请使用以下代码,
System.out.println("SEEN BY: " + driver.findElement(By.xpath("//div[contains(@class,'extra-padding visible-xl')]//*[@data-element='encounter-details-seen-by']")).getText());
对于第二个 SEEN BY ,
System.out.println("SEEN BY: " + driver.findElement(By.xpath("//div[contains(@class,'fixed col-seen-by')]//*[@data-element='encounter-details-seen-by']")).getText());
答案 1 :(得分:1)
要检索 Encounter详细信息,您可以为标题创建两个列表,为值创建另一个列表 >并按如下方式打印:
List<WebElement> items = driver.findElements(By.xpath("//div[@class='inline-flex-group-v2']//div[contains(@class,'item') and contains(@class,'gutter-sm') and contains(@class,'box-fixed')]/label"));
List<WebElement> value = driver.findElements(By.xpath("//div[@class='inline-flex-group-v2']//div[contains(@class,'item') and contains(@class,'gutter-sm') and contains(@class,'box-fixed')]//div[starts-with(@data-element,'encounter-details-')]"));
for(int i=0; i<items.size(); i++)
System.out.println(items.get(i).getText() + " value is : " + value.get(i).getText());
答案 2 :(得分:1)
根据您的要求,您可以将 cssSelector 用于注释类型。
您可以尝试代码:
System.out.println("NOTE TYPE: " + driver.findElement(By.cssSelector("div[class$='col-extra-padding'] div")).getText());
如果您仍想使用 Xpath ,那么您可以尝试代码
Xpath:
//label[text()='Note type']/following-sibling::div
代码:
System.out.println("NOTE TYPE: " + driver.findElement(By.xpath("//label[text()='Note type']/following-sibling::div")).getText());
有两个 Seen By :
对于第一个,您可以将 cssSelector 写为:
div[class*='col-extra-padding']>div[data-element$='-seen-by']
代码
System.out.println("First Seen By: " + driver.findElement(By.cssSelector("div[class*='col-extra-padding']>div[data-element$='-seen-by']")).getText());
Xpath 将是:
//div[contains(@class,'col-extra-padding')]/child::div[@data-element='encounter-details-seen-by']
代码:
System.out.println("First Seen By: " + driver.findElement(By.xpath("//div[contains(@class,'col-extra-padding')]/child::div[@data-element='encounter-details-seen-by']")).getText());
对于第二 看到,您可以将 cssSelector 写为:
cssSelector :
div[class*='box-padding-Tmd'] div[class$='col-seen-by'] div
代码
System.out.println("Second Seen By: " + driver.findElement(By.cssSelector("div[class*='box-padding-Tmd'] div[class$='col-seen-by'] div")).getText());
Xpath:
//div[contains(@class,'box-padding-Tmd')]/child::div[contains(@class,'col-seen-by')]/child::div
代码
System.out.println("Second Seen By: " + driver.findElement(By.xpath("//div[contains(@class,'box-padding-Tmd')]/child::div[contains(@class,'col-seen-by')]/child::div ")).getText());
答案 3 :(得分:0)
在定位器中添加父节点以缩小encounter type
和note type
的搜索范围。
你在SEEN BY
上空的原因,我想它在页面上是不可见的,或者你的定位器找不到多个元素,第一个元素没有文本内容。如果它不可见,您可以使用getAttribute("innerText")
获取元素的文本内容。
System.out.println("Encounter details");
System.out.println("ENCOUNTER TYPE: " + driver
.findElement(By.cssSelector("div.col-encounter-type div[data-element='encounter-details-encounter-type']"))
.getText());
System.out.println("NOTE TYPE: " + driver
.findElement(By.cssSelector("div.col-extra-padding div[data-element='encounter-details-encounter-type']"))
.getText());
System.out.println("DATE: " + driver
.findElement(By.cssSelector("div[data-element='encounter-details-date']"))
.getText());
System.out.println("AGE OF ENCOUNTER: " + driver
.findElement(By.cssSelector("div[data-element='encounter-details-age']"))
.getText());
System.out.println("SEEN BY: " + driver
.findElement(By.cssSelector("div[data-element='encounter-details-seen-by']"))
.getAttribute("innerText"));