如何解决如何为包含:: before标记的元素编写xpath
我试图单击一个选择元素,其后跟随::before
标签。谁能帮我写下面的HTML的XPath或CSSselector路径
下面是我编写用来单击月份的XPath
driver.findElement(By.xpath("//div[contains(@class,'pika-single is-bound left-aligned bottom-aligned')]/div[contains(@class,'pika-lendar')]/div[contains(@class,'pika-title']/div[1]/select[contains(@class,'pika-select pika-select-month')]"));
解决方法
Pseudo Elements
CSS pseudo-element用于设置元素的指定部分的样式。它可以用来:
- 设置元素的第一个字母或行的样式
- 在元素内容之前或之后插入内容
::之前
::before
是一个伪元素,它允许您将内容从CSS插入到页面上(不需要在HTML中)。虽然最终结果实际上不在DOM中,但它在页面上的显示方式好像是这样。
您可以在以下位置找到几个相关的详细讨论:
- How locate the pseudo-element ::before using Selenium Python
- Not able to get element by xpath inside div with ::before
此用例
由于日期选择器是html-select菜单,因此必须使用Select Class 。此外,由于您必须与drop-down-menu进行交互,因此必须为elementToBeClickable()
诱导WebDriverWait。
要从dropdown中选择文本为 January 的<option>
,可以使用以下Locator Strategies中的任何一个:
-
使用 cssSelector :
WebElement elem = new WebDriverWait(driver,20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("select.pika-select.pika-select-month"))); Select mySelect = new Select(elem); //selecting the item January by value mySelect.selectByValue("0");
-
使用 xpath 并在一行中:
//selecting the item January by visible text new Select(new WebDriverWait(driver,20).until(ExpectedConditions.elementToBeClickable(By.xpath("//select[@class='pika-select pika-select-month']")))).selectByVisibleText("January");
您可以在以下位置找到几个相关的详细讨论:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。