简体   繁体   English

如何使用 Selenium 和 Java 构建 xpath 或 css 定位器来识别元素

[英]How to construct a xpath or css locator to identify the element using Selenium and Java

Here is my Html:这是我的HTML:

<div class="x-form-item " tabindex="-1" id="ext-gen118">
    <label for="ext-comp-1060" style="width:40px;" class="x-form-item-label" id="ext-gen119">Name:</label>
    <div class="x-form-element" id="x-form-el-ext-comp-1060" style="padding-left:45px">
        <div class="x-form-field-wrap x-form-field-trigger-wrap x-trigger-wrap-focus" id="ext-gen120" style="width: 168px;">
            <input type="text" size="16" autocomplete="off" id="ext-comp-1060" name="ext-comp-1060" class="x-form-text x-form-field x-form-focus" style="width: 143px;" title="">
            <span class="x-form-twin-triggers" id="ext-gen121">
                <img src="/mco/extjs/resources/images/default/s.gif" alt="" class="x-form-trigger x-form-clear-trigger" id="ext-gen122" style="display: none;">
                <img src="/mco/extjs/resources/images/default/s.gif" alt="" class="x-form-trigger x-form-search-trigger" id="ext-gen123">
            </span>
        </div>
    </div>
    <div class="x-form-clear-left"></div>
</div>

I have tried below id "ext-comp-1060 is dynamic so that I cannot use.我已经尝试过以下 ID“ext-comp-1060 是动态的,因此我无法使用。

xpath = "//div[@class='x-form-field-wrap x-form-field-trigger-wrap x-trigger-wrap-focus']//input[@class='input.x-form-text.x-form-field.x-form-focus']"

css = "input.x-form-text.x-form-field.x-form-focus"

In My application same input text field I have to use two times one after other first time it's working I have tried like this:在我的应用程序相同的输入文本字段中,我必须一次又一次地使用两次,第一次工作时我试过这样的:

Wait wait = new FluentWait<WebDriver>(driver)
                .withTimeout(10, TimeUnit.SECONDS)
               .pollingEvery(3, TimeUnit.SECONDS);
       wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//label[contains(text(),'Name:')]")));
       ((JavascriptExecutor) driver).executeScript("arguments[0].click();", labelName);

searchName =@FindBy(css = "input.x-form-text.x-form-field.x-form-focus")
private WebElement searchByName;

searchByName.sendKeys(name);

But At same time second time it's not working但同时第二次它不起作用

Your webelement is dynamic so you can look for partial matching for element's attribute like Id , class and Name您的webelement是动态的,因此您可以查找元素属性的部分匹配,例如IdclassName

Wait for element等待元素

    By byCss=By.cssSelector("input[id^='ext-comp-']");
    By byXpath= By.xpath("//label[.='Name:']//input");

    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(ExpectedConditions.elementToBeClickable(byCss));

    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(ExpectedConditions.elementToBeClickable(byXpath));

Find element and Click:查找元素并单击:

    WebElement element = driver.findElement(byCss); 


    WebElement element = driver.findElement(byXpath);

    element.click(); 

To click() within the desired element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies :要在所需元素中click() ,您需要为elementToBeClickable()引入WebDriverWait ,您可以使用以下任一定位器策略

  • cssSelector : cssSelector

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label.x-form-item-label[id^='ext-gen'][for^='ext-comp-']"))).click();
  • xpath : xpath

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@class='x-form-item-label' and starts-with(@id, 'ext-gen')][starts-with(@for, 'ext-comp-') and text()='Name:']"))).click();

Does only the numbers change or the whole word?是只更改数字还是整个单词? If only the numbers change you can use this as Css locator:如果只有数字发生变化,您可以将其用作 Css 定位器:

input[id^='ext-comp']

我使用 contains 更新了您的 xpath,试试这个:

xpath = "//div[contains(@class, 'x-form-field-wrap']/input[contains(@class, 'x-form-text']"

Use the following XPATH which will identify the input element with reference to label tag Name:使用以下XPATH将参考标签标签Name:标识输入元素Name:

//label[text()='Name:']/following-sibling::div[1]//input //label[text()='Name:']/following-sibling::div[1]//输入

To handle dynamic element Induce WebDriverWait and wait for elementToBeClickable()处理动态元素 Induce WebDriverWait并等待elementToBeClickable()

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[text()='Name:']/following-sibling::div[1]//input"))).sendKeys("Hello");

You can try below given xpath.您可以在下面给定的 xpath 中尝试。

//div[@class='x-form-element']//child::input[@type='text' AND size='16']

I am accessing the input field using type and size attribute of the web element through a parent level element.我正在通过父级元素使用 web 元素的 type 和 size 属性访问输入字段。

Hope this will solve your problem.希望这能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium-Xpath-Java或CSS定位器 - Selenium-Xpath-Java or CSS locator 如何在下面的HTML代码中标识定位器,以及如何在Selenium中为此编写xpath? - How to identify a locator in below Html code and how to write a xpath for this in selenium? 如何通过Java Selenium中的XPath标识文本元素为Google登录 - How to identify the element with text as Sign in with Google through xpath in java selenium 如何在 xpath 中传递变量字符串以识别 Selenium 和 Java 中的元素 - How to pass a variable string within the xpath to identify an element in Selenium and Java 如何使用 Selenium 和 Java 识别元素 - How to identify the element using Selenium and Java 即使xpath相同,如何识别硒中的元素 - How to identify an element in selenium even if xpath is same 如何在 selenium/java 中获取实际的 Xpath/定位器表达式 - How to get the actual Xpath/ locator expression in selenium/ java 我如何使用 DOM 定位器使用 selenium webdriver java 来定位 Web 元素 - How can i locate a web element by using DOM locator using selenium webdriver java 如何通过类名识别元素并使用Selenium和Java单击它? - How to identify an element through classname and click on it using Selenium and Java? 如何使用Java和Selenium WebDriver识别元素中的方括号是否存在 - How to identify if brackets in the element are present or not using Java and Selenium WebDriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM