In the webpage https://cloudwise.nl/dit-is-cloudwise/alle-cloudwisers/directie/ I'm trying to get all users' names using for loop.
What I have tried so far is:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(#class,'inner')]/h3"))).getText();
and
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(#class,'inner')]/h3"))).getAttribute("innerHTML");
But all of them gets text Directie instead of users' name. I think it's because of the users' name is in a header <h3> tag and it just ignores it. How can I get the users' name within a header tag?
You were close enough. visibilityOfElementLocated() will always return you the first matching element, where as you need all the matching elements.
Solution
To print the list of users you need to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use Java8 stream() and map() and you can use either of the following Locator Strategies:
Using cssSelector:
System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.inner h3"))).stream().map(element->element.getText()).collect(Collectors.toList()));
Using xpath:
System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[contains(#class,'inner')]//h3"))).stream().map(element->element.getAttribute("innerHTML")).collect(Collectors.toList()));
Related
Element HTML:
Inbox
What I tried:
driver.findElement(By.cssSelector("a[text='Log out']"));
then
driver.findElement(By.xpath("//a[.='Log out']"));
Element snapshot:
HTML
driver.findElement(By.linkText("Log out"));
Something like that should work, you should provide the page html for a better response.
My response is based on the fact that in your first try you are saying text='Log out'.
findElement in selenium works with locatorStrategies (By.cssSelector/By.linkText...) the one that i used (linkText) search in all the anchor tags that are found in the pages () and analyze the text inside the tag, if the text is matched the driver will find the element.
Let me know if it works, otherwise provide me the web page html snippet.
I've seen the actual screen, you must change Log out with Inbox
driver.findElement(By.linkText("Inbox"));
Given the HTML:
Inbox
You need to take care of a couple of things here as follows:
Within cssSelector doesn't supports the :contains("text") method
Within xpath for exact text matches you need to use text()
Solution
To identify the element you can use either of the following locator strategies:
Using linkText:
WebElement element = driver.findElement(By.linkText("Log out"));
Using cssSelector:
WebElement element = driver.findElement(By.cssSelector("a[href$='INBOX'][title='View the Inbox']"));
Using xpath:
WebElement element = driver.findElement(By.xpath("//a[text()='Inbox' and #title='View the Inbox']"));
I am trying to find an element By.cssSelector("#e1"). On this website : SALESFOCE.COM.
I am trying to find an element on this first button(image below), which is a <div>.
From java, I tried By.xPath and By.cssSelector but every time I am getting No such element exception.
I also used Implicit and explicit wait still getting same exception.
But if I find using inspect element than it highlights the element.
The element Salesforce is under iframe you need to switch to iframe first in order to access the element.
Use WebdriverWait() and wait for frameToBeAvailableAndSwitchToIt() and use following locator to identify.
Use WebdriverWait() and wait for elementToBeClickable() and use following locator to identify.
driver.get("http://ww1.salesfoce.com/")
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("#rs>iframe")));
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Salesforce']"))).click();
The element with the text as Salesforce is within an <iframe>
Solution
To click on the element with text as Salesforce within the website 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:
driver.get("http://ww1.salesfoce.com/");
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(#src, 'https://www.google.com/afs/ads?adtest')]")));
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Salesforce']"))).click();
I want to get a text which is inside a div. I tried to find help from other questions, but most of them say .getText() which doesnt exist anymore.
This is the inspect:
Im using a chrome extension with which i can copy out the XPath, but it doesnt reads the text inside the div. It copies me this here:
//div[#class='mud-alert-message']
To print the text Die Datei... you can use either of the following Locator Strategies:
Using cssSelector and getAttribute("innerHTML"):
System.out.println(wd.findElement(By.cssSelector("div.mud-alert-message")).getAttribute("innerHTML"));
Using xpath and getText():
System.out.println(wd.findElement(By.xpath("//div[#class='mud-alert-message']")).getText());
Ideally, to extract the text you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
Using cssSelector and getText():
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.mud-alert-message"))).getText());
Using xpath and getAttribute("innerHTML"):
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='mud-alert-message']"))).getAttribute("innerHTML"));
I believe is something like this:
WebElement element = new WebDriverWait(driver, ...).until(ExpectedConditions.elementToBeXXX(By.LOCATOR_TYPE));
String text = element.getText();
I have a div element which looks like below.
I identify this element with the following xpath.
//*[contains(#class,'ce-component-title')]
Selenium identifies this element and loads the WebElement object. But when I go to get its text, I'm just getting a "." as shown below instead of getting "Purchase to pay process". What am I doing wrong here? I checked the chrome console and there's no other element matching this xpath.
Any help would be much appreciated.
As per the HTML you have shared the element is an Angular element so you have to induce WebDriverWait for the desired element to be visible and you can use either of the following solutions:
cssSelector:
String myString = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div[ng-if*='getTitle']>div.ce-component-title"))).getAttribute("innerHTML");
xpath:
String myString = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(#ng-if,'getTitle')]/div[#class='ce-component-title flex text-overflow ng-binding']"))).getAttribute("innerHTML");
I think your xpath (//*[contains(#class,'ce-component-title')]) returns more than one element, and the element you need is not the first one.
Try using console of your browser, to verify how many elements this xpath returns.
I can help you if you give me url to this page.
Would you mind helping me to understand how I can handle this dynamic ID?
Here are cases which I have already tried:
driver.findElement(By.xpath("//input[contains(#id,'Username')]")).sendKeys("aaa");
driver.findElement(By.xpath("//input[starts-with(#id,'undefined-undefined-Username-')]")).sendKeys("aaa");
driver.findElement(By.xpath("//*[#type='text']")).sendKeys("aaa");
No way to find that element.
As per the HTML you have shared the element is a dynamic element . To invoke click() on the desired element you can use either of the following solutions:
cssSelector:
driver.findElement(By.cssSelector("label[for^='undefined-undefined-Username-']")).sendKeys("aaa");
xpath:
driver.findElement(By.xpath("//label[starts-with(#for,'undefined-undefined-Username-')][contains(.,'Username')]")).sendKeys("aaa");
Update
As the element is dynamic you may need to induce WebDriverWait for the desired element to be clickable as follows:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label[for^='undefined-undefined-Username-']"))).sendKeys("aaa");
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[starts-with(#for,'undefined-undefined-Username-')][contains(.,'Username')]"))).sendKeys("aaa");