How to locate the element as shown in the image - java

The drop down is already selected by value 1 and its not locating through the id or name. How to find the xpath to locate as for the image?
HTML:
<select name="ctl05$ddlSelectBox" onchange="javascript:setTimeout('__doPostBack(\'ctl05$ddlSelectBox\',\'\')', 0)" id="ctl05_ddlSelectBox" style="height:21px;width:180px;">
<option selected="selected" value="-1">--Select Box--</option>
<option value="2742">Box_H026_01</option>
Image:

The desired element is a JavaScript enabled <select> node. So locate the element you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("select[name$='ddlSelectBox'][onchange*='ddlSelectBox']")));
Using XPATH:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[contains(#name, 'ddlSelectBox') and contains(#onchange, 'ddlSelectBox')]")));

Related

How to identify the element using Selenium and Java

I'm trying to identify the element. I want to identify the "address your concerns.". I am able to reach up to div tag but i need to reach the text in the div tag.
HTML:
<span class="Linkify">
<div>
<div>
address your concerns.
</div>
</div>
</span>
I am trying to identify the text "address your concerns" so can anyone help me with this in chrome browser, I am trying?
To identify the element using Selenium you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
cssSelector:
WebElement cssElement = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("span.Linkify > div > div")));
xpath:
WebElement xpathElement = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[#class='Linkify']/div/div[contains(., 'address your concerns')]")));
Try doing it using XPath to locate your div element, something like this "//span[#class='Linkify']/div/div" and then use getText() method on this div to get your text.

How to click on web element to display datepicker?

I can not locate element for this box to click on to get the date picker to popup
Any help would be appreciated
when I inspect element this is what I see...Xpath does not work either
<input ng-disabled="viewOnly" class="formInput effectiveDates ng-pristine ng-isolate-scope hasDatepicker ng-empty ng-invalid ng-invalid-required ng-not-modified ng-touched" autocomplete="off" name="effectiveStartDate" ng-required="true" placeholder="Select/Enter date(mm/dd/yyyy)" type="search" ng-model="authObj.authStartDate" value="" ng-keydown="preventUserEnterDateInfo($event)" datepicker="" minimumdate="02/27/2020" ng-change="effectiveDateChange(authObj.authStartDate)" ng-class="(authListForm.$submitted && authListForm.effectiveStartDate.$invalid)?'reqd':''" id="dp1582663746532" required="required">
The desired element is an Angular element so to locate/click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.formInput.effectiveDates.ng-pristine.ng-isolate-scope.hasDatepicker.ng-empty.ng-invalid.ng-invalid-required.ng-not-modified.ng-touched[name='effectiveStartDate'][placeholder*='Enter date']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='formInput effectiveDates ng-pristine ng-isolate-scope hasDatepicker ng-empty ng-invalid ng-invalid-required ng-not-modified ng-touched' and #name='effectiveStartDate'][contains(#placeholder, 'Enter date')]"))).click();
You can try click using JavascriptExecutor.
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
or you can try click using coordinates:
Actions builder = new Actions(driver);
builder.moveToElement(element, 10, 25).click().build().perform();
I noticed that the 'type'-tag is set to Search. If you want a browser to recognize it as a Date-Input, change it to type="date". The browser itself will invoke its default datepicker.

Can't select option in Select with Selenium

I am having an issue when I try to select an option in a <select> in Selenium.
Select select = new Select(element);
actions.moveToElement(element);
select.selectByValue("100000");
This simply gives me ElementClickIntercepted.
Trying to click on it also gives me ElementClickIntercepted.
Trying to click on it with JS gives me a NullPointerException.
I can very easily select it in Firefox with the element selector so nothing is on top of the select that prevents me from clicking it.
What is intercepting the click? usually when it's because an element is overlaying another, it will tell me in the test results, but here it doesn't.
<div class="pull-left">
<select name="nb" class="form-control">
<option value="10">10</option><option value="20">20</option><option value="50">50</option><option value="100000">All</option>
</select>
</div>
Select xPath:
//select[#name="nb"]
And it is the only select on the page.
Try this:
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//select[#name='nb']")));
Select select = new Select(element);
actions.moveToElement(element);
select.selectByValue("100000");
As the element is a <select> element ideally you need to use Select class. To invoke click() on the option with value as 1000 you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("select.form-control[name='nb']")))).selectByValue("100000");
xpath:
new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//select[#class='form-control' and #name='nb']")))).selectByValue("100000");

How to click the element from the table Search by specific text

How to click on element search by text within table. I have tried some code but its not working.
HTML:
<td _ngcontent-c8="" class="align-middle cursorPoint" tabindex="0">Shelton</td>
I want to click on this <tr> which having text Shelton in it.
The desired element is an Angular element so to locate and click() the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategy:
xpath 1:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[#class='align-middle cursorPoint' and text()='Shelton']"))).click();
xpath 2:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[text()='Shelton']"))).click();
Update
To achieve the same in a step-wise manner:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[#class='align-middle cursorPoint' and text()='Shelton']")));
elem.click();

Unable to locate the button or link using either xpath, id, name or css selector

Unable to locate the element using id/name/xpath/CSSSelector
Tried the below codes and both failed to give response
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id=\'form\']/p/button/span")));
driver.findElement(By.xpath("//*[#id=\'form\']/p/button/span")).click();
and
WebElement checkout = driver.findElement(By.xpath("//[#id=\'form\']/p/button/span"));
checkout.click();
HTML
<button type="submit" name="processCarrier" class="button btn btn-default standard-checkout button-medium" style="">
<span> Proceed to checkout <i class="icon-chevron-right right"></i> </span>
</button>
Try Following CSS Selector.
WebElement checkout = driver.findElement(By.cssSelector("button.standard-checkout span"));
checkout .click();
Or yon can use WebDriverWait and Css Selector.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.standard-checkout span")));
element.click()
Probably you are getting org.openqa.selenium.InvalidSelectorException because you should use * after // to match any node(tag) who has id=form or the specific tag name.
change it to //*[#id='form']/p/button/span
Or use more specific like
xpath : //button[#name='processCarrier'] equivalent CSS : button[name='processCarrier']
And use implicit/explicit wait to make element available in DOM to perform actions.
Presumably you will invoke click() on the <button> element, so you need to induce WebDriverWait for the desired element to be clickable and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.standard-checkout.button-medium[name='processCarrier']>span"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='button btn btn-default standard-checkout button-medium' and #name='processCarrier']/span"))).click();

Categories