[英]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
是动态的,因此您可以查找元素属性的部分匹配,例如Id
、 class
和Name
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.