如何通过Selenium使用xpath定位元素
代码试用:
By.xpath("//*[contains(@class,'a-size-mini a-spacing-none a-color-base s-line-clamp-2') and contains(text(), \"Men's Downshifter 9 Training Shoes\")]"));HTML:
<h2 class="a-size-mini a-spacing-none a-color-base s-line-clamp-2">
<a class="a-link-normal a-text-normal" href="/Nike-Downshifter-Running-Shoes-Anthracite/dp/B07H84PMGS/ref=sr_1_3?dchild=1&keywords=NIKE+TRAINERS+FOR+MEN&qid=1578766779&sr=8-3">
<span class="a-size-base-plus a-color-base a-text-normal">Men's Downshifter 9 Training Shoes</span>
</a>
</h2>
发布于 2020-01-12 02:31:18
闭包]
By.xpath("//*[contains(@class,'a-size-mini a-spacing-none a-color-base s-line-clamp-2') and contains(text(), \"Men's Downshifter 9 Training Shoes\")]]"));替代方案:
//h2[.='Men's Downshifter 9 Training Shoes']//span[.='Men's Downshifter 9 Training Shoes']/ancestor::h2[1]//h2[./a[.='Men's Downshifter 9 Training Shoes']]发布于 2020-01-12 05:37:19
要定位该元素,可以使用以下任一基于xpath的Locator Strategies
使用xpath和text()的
By.xpath(“//spantext()=\”男子Downshifter 9 Training Shoes\""));
contains():类(“//span@class=‘a-size-base-plus a-color-base a-text-By.xpath’并包含(.,'s Downshifter 9 Training Shoes')"));
<a> tag:class(“//a@By.xpath=‘a-link-normal a-text-normal’并以(@href,'/Nike-Downshifter-Running-Shoes-Anthracite/dp/')/span@class='a-size-base-plus a-color-base a-text-normal‘and contains(.,'Downshifter 9 Training Shoes’)‘)开始;
https://stackoverflow.com/questions/59697389
复制相似问题