【问题标题】:chromedriver select item with h1 and p sections in htmlchromedriver在html中选择带有h1和p部分的项目
【发布时间】:2017-03-08 05:09:53
【问题描述】:
我有这个文章部分,其中 h1 和 p 部分代表产品名称和颜色。使用chromedriver,当产品名称和颜色名称两个条件符合预期时,有什么方法可以点击这个特定项目?我试图使用 find_element_by_link_text 但它似乎还没有工作。enter image description here
【问题讨论】:
标签:
python
css
google-chrome
selenium
selenium-chromedriver
【解决方案1】:
以下 xpath 应该能够帮助您根据 h1 和 p 标签属性选择特定项目:
//p/a[text()='Red']/parent::p/preceding-sibling::h1/a[text()='Script Logo 6-Panel']/parent::h1/preceding-sibling::a[1]
告诉我,它是否适合你。
【解决方案2】:
您可以使用 xpath 来检查条件。如果找到满足条件的元素,则点击它
// xpath for element satisfying the condition
String xpath = "//h1/a/[text()= 'Script Logo 6-panel']/../following-sibling::p[1]/a[text()='Red']"
// find the element
List<WebElement> elements = driver.findElements(By.xpath(xpath));
// If element is found, click on it.
if (elements.size()!=0) {
elements.get(0).click();
}
上面的代码是java。但同样可以在python中实现。