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

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();

Related

Selenium can't find iframe after postback inside the iframe after finding one element inside the frame

I am trying to create a test where I have to fill out some information inside an iframe. Getting access to the iframe work fine and I can fill out information inside the frame. The issue is that when I find out a element 'A' it has a postback attached to it which reloads the content inside the iframe, to find out another element 'B'. So i am not able to find that element.I am getting below error:
org.openqa.selenium.WebDriverException: unknown error: Element <iframe style="overflow-x:hidden;" id="t5" height="1350" frameborder="0" width="98%" src="https://edata.ndtv.com/coronavirus/table/india_table.html?shgraph=1&days=7" cd_frame_id_="7da8f2aea5a580b3a6e90a9d5016fa0d">...</iframe> is not clickable at point (554, 7). Other element would receive the click: <div class="topnav2014" style="border-bottom: none;">...</div>
(Session info: chrome=85.0.4183.83)
Here are my observations: When I first locate the iframe it looks like this:
<iframe style="overflow-x:hidden;" id="t5" height="1350" frameborder="0" width="98%" src="https://edata.ndtv.com/coronavirus/table/india_table.html?shgraph=1&days=7">
After the postback has occurred it looks like this:
<iframe style="overflow-x:hidden;" id="t5" height="1350" frameborder="0" width="98%" src="https://edata.ndtv.com/coronavirus/table/india_table.html?shgraph=1&days=7" cd_frame_id_="a5006acf28d8c288313681ab9ad7a4fa">
I can easily find element A:
But element B i am not able to find
The code fails when I try to get hold of the iframe element.
How can I get hold of the iframe again, after the postback inside the frame?
I have tried this suggestion also but it is not working
//Ensure that you are back to the base frame
driver.SwitchTo().DefaultContent();
//SwitchTo the intended frame
driver.SwitchTo().Frame(driver.FindElement(By.XPath("//iframe[contains(#src,'<removed for clearity>')]")));
Use a driver.executescript() for the first problem since another element is receiving the click.
element = driver.findElement(By.id(""));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);
This error message...
org.openqa.selenium.WebDriverException: unknown error: Element <iframe style="overflow-x:hidden;" id="t5" height="1350" frameborder="0" width="98%" src="https://edata.ndtv.com/coronavirus/table/india_table.html?shgraph=1&days=7" cd_frame_id_="7da8f2aea5a580b3a6e90a9d5016fa0d">...</iframe> is not clickable at point (554, 7). Other element would receive the click: <div class="topnav2014" style="border-bottom: none;">...</div>
(Session info: chrome=85.0.4183.83)
...implies that the WebDriverException was raised as you tried to invoke click() on the <iframe> element.
Factually, instead of clicking on the <iframe> element you would invoke click() on an element within the <iframe>. Moreover, as the the desired element is within a <iframe> so you have to:
Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
Induce WebDriverWait for the desired elementToBeClickable.
You can use either of the following Locator Strategies:
Using xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe_xpath")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("element_xpath"))).click();
References
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?
I am able to extract the data of the active cases rise and fall for every state. There is a single frame which contains both the locators shared by you. Check below working code.
driver.get("https://www.ndtv.com/coronavirus");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.className("tab-wrapper")));
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(#src,'https://edata.ndtv.com/coronavirus/table/india_table.html?shgraph=1')]")));
//cases down
List<WebElement> eleCasesUp = driver.findElements(By.xpath("//tr//td[3]//p//span[#class='data-up']"));
List<String> casesUpList = new ArrayList<String>();
for (WebElement element : eleCasesUp) {
casesUpList.add(element.getText());
}
//cases up
List<WebElement> eleCasesDown = driver.findElements(By.xpath("//tr//td[3]//p//span[#class='data-down']"));
List<String> casesDownList = new ArrayList<String>();
for (WebElement element : eleCasesDown) {
casesDownList.add(element.getText());
}
System.out.println("Cases Up List -->" + casesUpList);
System.out.println("Cases Down List -->" + casesDownList);

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 to locate and click the select boolean check box using Selenium and Java

I am trying to click primefaces select boolean check box using id but i could not able to click I'm getting some exception caused by:
org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.id: runDARtest1
I increased second 30 also but again getting same issues
driver.get("http://localhost:5080/MOIA2-WEB/");
driver.findElement(By.id("moia2Form:loginForm:tabViedID:userName")).sendKeys("cherie.chen");
driver.findElement(By.id("moia2Form:loginForm:tabViedID:password")).sendKeys("Password1");
driver.findElement(By.id("moia2Form:loginForm:tabViedID:loginButton")).click();
driver.findElement(By.id("moia2Form:lnkMarketClearing")).click();
driver.findElement(By.id("moia2Form:system_Result_runinfo")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("runDARtest1"))).click();;
Xhtml code: select boolean check box there in inside the table
<tr>
<td><label class="switch"> <h:selectBooleanCheckbox
class="runDARclass"
value="#{systemResultManagedBean.runDAR}" id="runDAR" />
<span class="slider round" id="runDARtest1"></span>
</label></td>
<td><p:spacer width="5px" /><span>DAR</span></td>
</tr>
The desired element is a dynamic element so to click() on the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label.switch span.slider.round#runDARtest1"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#class='switch']//span[#class='slider round' and #id='runDARtest1']"))).click();

How to locate element under <div></div> tag in selenium

There is a simple div tag with text. How to build xpath from the below tag.
WebElement element = driver.findElement(By.xpath("//div[#id='el2']"));
<DIV align=”left” id=e1>Insured</DIV>
Actions action = new Actions(driver);
//To mouseover on main menu
action.moveToElement(element).build().perform();
Since the id is changing every time, I need to use the text in a div tag
Try this below Xpath if your ID keeps changing. This should work fine.
WebElement element = driver.findElement(By.xpath("//DIV[contains(text(), 'Contacts')]"))
OR
WebElement element = driver.findElement(By.xpath("//DIV[text()[contains(.,'Contacts')]]"))

Selenium Web Driver - How to handle dynamic in href link

Below is the HTML:
Result
I have dynamic value here, keep changing when page refreshes! how to handle this. Please advise.
Driver.findElement(By.xpath("//a[#href='#report-adhoc/**32|229**/result']")).click();
Though the value is dynamic,
If the linkText Result is unique within the HTML DOM you can use the Link Text inducing WebDriverWait for the element to be clickable as follows:
linkText:
new WebDriverWait(Driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Result"))).click();
If the linkText Result is not unique within the HTML DOM you can use the XPath inducing WebDriverWait for the element to be clickable as follows:
xpath:
new WebDriverWait(Driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(#href,'report-adhoc') and contains(.,'Result')]"))).click();

Categories