如何为涉及 ::before 标记的元素编写 xpath [英] How to write the xpath for an element where ::before tag is involved
问题描述
我正在尝试单击后跟 ::before
标记的选择元素.任何人都可以帮我为下面的 HTML 编写 XPath 或 CSSselector 路径
I am trying to click a select element which is followed by ::before
tag. Can anybody please help me to write the XPath or the CSSselector path for the below HTML
下面是我写的点击月份的XPath
Below is the XPath that I have written to click on the month
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')]"));
推荐答案
伪元素
CSS 伪元素 用于为元素的指定部分设置样式.它可以用于:
Pseudo Elements
A CSS pseudo-element is used to style specified parts of an element. It can be used to:
- 为元素的第一个字母或行设置样式
- 在元素内容之前或之后插入内容
::before
是一个伪元素,它允许您将内容从 CSS 插入页面(无需在 HTML 中).虽然最终结果实际上并不在 DOM 中,但它似乎在页面上显示.
::before
is a pseudo element which allows you to insert content onto a page from CSS (without it needing to be in the HTML). While the end result is not actually in the DOM, it appears on the page as if it is.
您可以在以下位置找到一些相关的详细讨论:
You can find a couple of relevant detailed discussion in:
- 如何定位伪元素::在使用 Selenium Python 之前
- 无法使用 ::before 在 div 内通过 xpath 获取元素
- How locate the pseudo-element ::before using Selenium Python
- Not able to get element by xpath inside div with ::before
作为日期选择器是一个 html-选择菜单你必须使用选择 类.此外,由于您必须与 下拉菜单 你必须诱导 WebDriverWait 用于 elementToBeClickable()
.
As the date picker is a html-select menu you have to use the Select Class. Moreover, as you have to interact with the drop-down-menu you have to induce WebDriverWait for the elementToBeClickable()
.
要从 January 的 ="show questions tagged 'dropdown'" rel="tag">dropdown 你可以使用你可以使用以下任一定位器策略:
To select the <option>
with text as January from the dropdown you can use you can use either of the following 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");
您可以在以下位置找到一些相关的详细讨论:
You can find a couple of relevant detailed discussions in:
这篇关于如何为涉及 ::before 标记的元素编写 xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!