Below code is not woking with Chrome browser - java

I have created a reusable function which clicks the check box of a particular row and returns the text of that row.
CheckBoxXpath-> private static final String XPATH_JOBRATECATEGORIES_CHECKBOX_LIST = "//kendo-grid-list//table/tbody/tr/td[1]/label";
RowXpath -> private static final String XPATH_JOBRATECATEGORIES_LIST = "//kendo-grid-list//table/tbody/tr/td[2]//div//div";
count-> 0 (I want to click only first row check box)
public String Select_CheckBox_Return_SelectedText(String CheckBoxXpath,String RowXpath, int Count) {
List<WebElementFacade> listOfCheckBox = findAll(By.xpath(CheckBoxXpath));
List<WebElementFacade> listOfrow = findAll(By.xpath(RowXpath));
if(listOfCheckBox.size()>Count) {
for (int i = 0; i <= Count; i++) {
listOfCheckBox.get(i).click();
String Actual=listOfrow.get(i).getText();
}
}else {
Assert.fail("Need to have more rows to fullfill the requirement");
return null;
}
return Actual;
}
This is working fine with Firefox browser, but not working with Chrome browser.
On debugging code is throwing exception on -> "listOfCheckBox.get(i).click();"
I am not able to understand why it is behaving so weired.
Need help. Thanks in advance.

You need to target the checkboxes, not the labels in your xpath:
//kendo-grid-list//table/tbody/tr/td[1]/input[#type='checkbox']

Related

Cached elements do not exist in DOM anymore

Like in similar issue, I use appium + java. Trying to select elements
In mobile application I am go to page, and after that, have many elements android.widget.ImageView(0), I need to select 6 (for example) such elements and go with other steps. Byt can select only one element and then get such exception:
org.openqa.selenium.StaleElementReferenceException: Cached elements 'By.id: com.company:id/selector_view' do not exist in DOM anymore
public GalleryPage choosePhotosFromAlbum(int count) {
List<MobileElement> photos = driver.findElementsById(elements.get("photo from gallery album selector"));
for (int i = 0; i < count; i++) {
photos.get(i).click();
}
return new GalleryPage(device);
}
I had the same issue. I think this happens because every time you click on a photo, the DOM changes. So, when you try to click on the second photo, the cached elements are not on the DOM anymore.
Try putting the photos inside the for cycle, like this:
public GalleryPage choosePhotosFromAlbum(int count) {
for (int i = 0; i < count; i++) {
List<MobileElement> photos = driver.findElementsById(elements.get("photo from gallery album selector"));
photos.get(i).click();
}
return new GalleryPage(device);
}
This way the list of photos is retrieved from the refreshed DOM on every cycle.
And by the way, you're not checking if the count is bigger than the photos list size, which can result in an OutOfBounds exception or similar.
You can use explicit wait to solve this problem.
public GalleryPage choosePhotosFromAlbum(int count)
{
List<MobileElement> photos = driver.findElementsById(elements.get("photo from gallery album selector"));
for (int i = 0; i < count; i++)
{
new WebDriverWait(driver,10).until(ExpectedConditions.presenceOfAllElementsLocatedBy("Your object property"));
photos.get(i).click();
}
return new GalleryPage(device);
}
Try enableMultiWindows appium capability set to true https://github.com/appium/appium/blob/master/docs/en/advanced-concepts/settings.md
Or you can try finding elements via WebDriverWait. In Appium (C#), when the driver tries to find native elements, it can sometimes throw a StaleElementReferenceException. Thus you can ignore this exception and wait until elements exist in DOM:
public ReadOnlyCollection<IWebElement> WaitElements(By selector)
{
var wait = new WebDriverWait(this.Driver, new TimeSpan(0, 0, 10));
ReadOnlyCollection<IWebElement> results = null;
try
{
wait.Until(driver =>
{
try
{
var elements = driver.FindElements(selector);
if (elements.Any())
{
results = elements;
return true;
}
}
catch (StaleElementReferenceException)
{
// ignore
}
return false;
});
}
catch (WebDriverTimeoutException)
{
throw new NoSuchElementException("Elements not found");
}
return results;
}

How to edit a Hyperlink in a Word Document using Apache POI?

So I've been browsing around the source code / documentation for POI (specifically XWPF) and I can't seem to find anything that relates to editing a hyperlink in a .docx. I only see functionality to get the information for the currently set hyperlink. My goal is to change the hyperlink in a .docx to link to "http://yahoo.com" from "http://google.com" as an example. Any help would be greatly appreciated. Thanks!
I found a way to edit the url of the link in a "indirect way" (copy the previous hyperlink, modify the url, delete the previous hyperlink and add the new one in the paragraph).
Code is shown below:
private void editLinksOfParagraph(XWPFParagraph paragraph, XWPFDocument document) {
for (int rIndex = 0; rIndex < paragraph.getRuns().size(); rIndex++) {
XWPFRun run = paragraph.getRuns().get(rIndex);
if (run instanceof XWPFHyperlinkRun) {
// get the url of the link to edit it
XWPFHyperlink link = ((XWPFHyperlinkRun) run).getHyperlink(document);
String linkURL = link.getURL();
//get the xml representation of the hyperlink that includes all the information
XmlObject xmlObject = run.getCTR().copy();
linkURL += "-edited-link"; //edited url of the link, f.e add a '-edited-link' suffix
//remove the previous link from the paragraph
paragraph.removeRun(rIndex);
//add the new hyperlinked with updated url in the paragraph, in place of the previous deleted
XWPFHyperlinkRun hyperlinkRun = paragraph.insertNewHyperlinkRun(rIndex, linkURL);
hyperlinkRun.getCTR().set(xmlObject);
}
}
}
This requirement needs knowledge about how hyperlinks referring to an external reference get stored in Microsoft Word documents and how this gets represented in XWPF of Apache POI.
The XWPFHyperlinkRun is the representation of a linked text run in a IRunBody. This text run, or even multiple text runs, is/are wrapped with a XML object of type CTHyperlink. This contains a relation ID which points to a relation in the package relations part. This package relation contains the URI which is the hyperlink's target.
Currently (apache poi 5.2.2) XWPFHyperlinkRun provides access to a XWPFHyperlink. But this is very rudimentary. It only has getters for the Id and the URI. It neither provides access to it's XWPFHyperlinkRun and it's IRunBody nor it provides a setter for the target URI in the package relations part. It not even has internally access to it's the package relations part.
So only using Apache POI classes the only possibility currently is to delete the old XWPFHyperlinkRun and create a new one pointing to the new URI. But as the text runs also contain the text formatting, deleting them will also delete the text formatting. It would must be copied from the old XWPFHyperlinkRun to the new before deleting the old one. That's uncomfortable.
So the rudimentary XWPFHyperlink should be extended to provide a setter for the target URI in the package relations part. A new class XWPFHyperlinkExtended could look like so:
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
/**
* Extended XWPF hyperlink class
* Provides access to it's Id, URI, XWPFHyperlinkRun, IRunBody.
* Provides setting target URI in PackageRelationship.
*/
public class XWPFHyperlinkExtended {
private String id;
private String uri;
private XWPFHyperlinkRun hyperlinkRun;
private IRunBody runBody;
private PackageRelationship rel;
public XWPFHyperlinkExtended(XWPFHyperlinkRun hyperlinkRun, PackageRelationship rel) {
this.id = rel.getId();
this.uri = rel.getTargetURI().toString();
this.hyperlinkRun = hyperlinkRun;
this.runBody = hyperlinkRun.getParent();
this.rel = rel;
}
public String getId() {
return this.id;
}
public String getURI() {
return this.uri;
}
public IRunBody getIRunBody() {
return this.runBody;
}
public XWPFHyperlinkRun getHyperlinkRun() {
return this.hyperlinkRun;
}
/**
* Provides setting target URI in PackageRelationship.
* The old PackageRelationship gets removed.
* A new PackageRelationship gets added using the same Id.
*/
public void setTargetURI(String uri) {
this.runBody.getPart().getPackagePart().removeRelationship(this.getId());
this.uri = uri;
PackageRelationship rel = this.runBody.getPart().getPackagePart().addExternalRelationship(uri, XWPFRelation.HYPERLINK.getRelation(), this.getId());
this.rel = rel;
}
}
It does not extend XWPFHyperlink as this is so rudimentary it's not worth it. Furthermore after setTargetURI the String uri needs to be updated. But there is no setter in XWPFHyperlink and the field is only accessible from inside the package.
The new XWPFHyperlinkExtended can be got from XWPFHyperlinkRun like so:
/**
* If this HyperlinkRun refers to an external reference hyperlink,
* return the XWPFHyperlinkExtended object for it.
* May return null if no PackageRelationship found.
*/
/*modifiers*/ XWPFHyperlinkExtended getHyperlink(XWPFHyperlinkRun hyperlinkRun) {
try {
for (org.apache.poi.openxml4j.opc.PackageRelationship rel : hyperlinkRun.getParent().getPart().getPackagePart().getRelationshipsByType(XWPFRelation.HYPERLINK.getRelation())) {
if (rel.getId().equals(hyperlinkRun.getHyperlinkId())) {
return new XWPFHyperlinkExtended(hyperlinkRun, rel);
}
}
} catch (org.apache.poi.openxml4j.exceptions.InvalidFormatException ifex) {
// do nothing, simply do not return something
}
return null;
}
Once we have that XWPFHyperlinkExtended we can set an new target URI using it's method setTargetURI.
A further problem results from the fact, that the XML object of type CTHyperlink can wrap around multiple text runs, not only one. Then multiple XWPFHyperlinkRun are in one CTHyperlink and point to one target URI. For example this could look like:
... [this is a link to example.com]->https://example.com ...
This results in 6 XWPFHyperlinkRuns in one CTHyperlink linking to https://example.com.
This leads to problems when link text needs to be changed when the link target changes. The text of all the 6 text runs is the link text. So which text run shall be changed?
The best I have found is a method which sets the text of the first text run in the CTHyperlink.
/**
* Sets the text of the first text run in the CTHyperlink of this XWPFHyperlinkRun.
* Tries solving the problem when a CTHyperlink contains multiple text runs.
* Then the String value is set in first text run only. All other text runs are set empty.
*/
/*modifiers*/ void setTextInFirstRun(XWPFHyperlinkRun hyperlinkRun, String value) {
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink ctHyperlink = hyperlinkRun.getCTHyperlink();
for (int r = 0; r < ctHyperlink.getRList().size(); r++) {
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR ctR = ctHyperlink.getRList().get(r);
for (int t = 0; t < ctR.getTList().size(); t++) {
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText ctText = ctR.getTList().get(t);
if (r == 0 && t == 0) {
ctText.setStringValue(value);
} else {
ctText.setStringValue("");
}
}
}
}
There the String value is set in first text run only. All other text runs are set empty. The text formatting of the first text run remains.
This works, but need more some steps to get text formatting correctly:
try (var fis = new FileInputStream(fileName);
var doc = new XWPFDocument(fis)) {
var pList = doc.getParagraphs();
for (var p : pList) {
var runs = p.getRuns();
for (int i = 0; i < runs.size(); i++) {
var r = runs.get(i);
if (r instanceof XWPFHyperlinkRun) {
var run = (XWPFHyperlinkRun) r;
var link = run.getHyperlink(doc);
// To get text: link for checking
System.out.println(run.getText(0) + ": " + link.getURL());
// how i replace it
var run1 = p.insertNewHyperlinkRun(i, "http://google.com");
run1.setText(run.getText(0));
// remove the old link
p.removeRun(i + 1);
}
}
}
try (var fos = new FileOutputStream(outFileName)) {
doc.write(fos);
}
}
I'm using these libraries:
implementation 'org.apache.poi:poi:5.2.2'
implementation 'org.apache.poi:poi-ooxml:5.2.2'

Selenium looping sendKeys

So I have gotten far enough to locate the textbox on facebook, and send a person a message. Now I want to see if I can create a loop to send multiple messages at once. My current problem is I have a for loop:
for (int i = 0; i < 20; i++) {
messageBox.sendKeys("My friend's name " + i + " ");
messageBox.sendKeys(Keys.ENTER);
}
However, I can only do this once. So It will print "My friend's name0" and then stop. Java says: element is not attached to the page document.
I am assuming that because i sent the message, some part of the Inspect Element has changed? Is there a way to solve this?
Get Friend's name from Excel sheet
Make a Method for get data from excel file:-
public static void fn_FetchExcelData(String FilePath,String ColumnName) throws IOException{
String WbookPath=FilePath;
Workbook WBookObj=fn_GetWorkbook(FilePath);
Sheet SheetObj=WBookObj.getSheetAt(0);
Row FstRowObj=SheetObj.getRow(0);
int cellCount=FstRowObj.getLastCellNum();
int columnNumber=0;
ArrayList<String> AL=new ArrayList<String>();
for(int j=0; j<cellCount;j++){
if(FstRowObj.getCell(j, Row.CREATE_NULL_AS_BLANK).getStringCellValue().trim().equalsIgnoreCase(ColumnName)){
columnNumber=j;
break;
}
}
int rowcnt=SheetObj.getLastRowNum();
for(int i=1;i<=rowcnt;i++){
Cell fstcellObj=SheetObj.getRow(i).getCell(columnNumber, Row.CREATE_NULL_AS_BLANK);
String ColumnVal=fstcellObj.getStringCellValue();
AL.add(ColumnVal);
call excel method & use it
fn_FetchExcelData("Your excel data path");
int j;
int size = AL.size();
for(j=0;j<size;j++)
{
System.out.println(AL.get(j));
messageBox.sendKeys(AL.get(j));
Nevermind i figured it out, I just added
WebElement messageBox = driver.findElement(By.xpath("//textarea[#class='uiTextareaAutogrow _552m']"));
wait.until(ExpectedConditions.visibilityOf(messageBox));
Again to the loop and it works. However it is VERY slow.

WebDriver script for clicking all links except one with particular name not working

I am in process of learning Selenium WebDriver 2 using Java/Junit with IntelliJ IDEA.
The sample page I am using for creating scripts is http://newtours.demoaut.com/
I am trying to create a script that will click on all the links on the page EXCEPT the "SUPPORT" link.
I've imported all the necessary libraries and
Here is the code that I tried to use but it gives an error:
public class AllLinksExceptSupport{
WebDriver driver= new FirefoxDriver();
String url="http://newtours.demoaut.com/";
#Before
public void loadhomepage()
{
driver.get(url);
#Test
public void allexceptregister(){
List<WebElement> alllinks = driver.findElements(By.tagName("a"));
for (int i =0; i<= alllinks.size()-1; i++){
String oflinks = alllinks.get(i).getText();
if(oflinks.equals("SUPPORT")){
continue;
}
alllinks.get(i).click();
}
}
}
Here is the error message:
org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up.
Can you please advise on what I'm doing wrong and what needs to be changed in the code/logic?
Thank you.
The issue you're encountering is that when you click a link, you're leaving the page. This makes the collection of elements in your List "stale".
This is an approach that should work for you. Of course, this assumes that all of the links have different link text.
List<WebElement> allLinks = driver.findElements(By.tagName("a"));
String ofLinks[] = new String[allLinks.size()];
for (int i = 0; i < allLinks.size(); i++){
if(!allLinks.get(i).getText().equals("SUPPORT")){
ofLinks[i] = allLinks.get(i).getText();
}
}
for (int i = 0; i < allLinks.size(); i++){
if (ofLinks[i] != null){
driver.findElement(By.LinkText(ofLinks[i])).click();
}
driver.findElement(By.linkText("Home")).click();
}

Creating a dynamic eclipse "Welcome" page button

i am wondering, whether the code to create the buttons shown at the eclipse "Welcome" page can be found somewhere.
When you create a new workspace in eclipse, a "Welcome" page is shown at the very beginning. The page shows different type of buttons like, "What is new", "Tutorials", ...
I want to use these type of buttons, but was not able to find the source code inside eclipse.
Does somebody know how to create such a button, with hide composites and dynimic components.
You can try and explore the org.eclipse.ui.internal.WorkbenchIntroManager class, in charge of building a ViewIntroAdapterPart, based on informations found in the ViewIntroAdapterSite
From getViewIntroAdapterPart():
* #return the <code>ViewIntroAdapterPart</code> for this workbench, <code>null</code> if it
* cannot be found.
*/
/*package*/ViewIntroAdapterPart getViewIntroAdapterPart() {
IWorkbenchWindow[] windows = this.workbench.getWorkbenchWindows();
for (int i = 0; i < windows.length; i++) {
IWorkbenchWindow window = windows[i];
WorkbenchPage page = (WorkbenchPage) window.getActivePage();
if (page == null) {
continue;
}
IPerspectiveDescriptor[] perspDescs = page.getOpenPerspectives();
for (int j = 0; j < perspDescs.length; j++) {
IPerspectiveDescriptor descriptor = perspDescs[j];
IViewReference reference = page.findPerspective(descriptor)
.findView(IIntroConstants.INTRO_VIEW_ID);
if (reference != null) {
IViewPart part = reference.getView(false);
if (part != null && part instanceof ViewIntroAdapterPart) {
return (ViewIntroAdapterPart) part;
}
}
}
}
return null;
}
Each perspective contributes to the IntroPart, based on its IPerspectiveDescriptor, if it includes a ViewIntroAdapterPart.
The ViewPart will create IIntroPart, which contains the graphical visible elements.

Categories