How to select a web element by text with Selenium WebDriver, Java
Css does not allow you do text based search. xpath is the only option there.
//div[contains(text(),'noreply@somedomain.com')]
How to find a Web Element by text in Selenium using Java?
You can try following code to identify this particular "bubble" class element:
WebElement elem = driver.findElement(By.xpath("//*[name()='circle'][contains(@style,'https://us.qlikcloud.com/node/i-0fc3fc8cc088b4fb7/sense/app/b5573a15-e951-45b5-81ed-92ef95c916bc/datamanager/datamanager#bubble-drop-shadow')]"));
Update #1:
driver.switchTo().frame("appFrame"); //need to switch to this frame before identifying elements present on frame with id "appFrame"
WebElement elem = driver.findElement(By.xpath("//div[@class='table-information']/div[@title='BaseAloca']"));
How to select an element based on other element in selenium webdriver
Try to locate using following xpath
-
//td[text()='My sample Test']/following-sibling::td/a[@title='Reject']
Explanation :-
Find the td
tag which having text as 'My sample Test'following-sibling
used to navigate the sibling which have reject button
Getting WebElement by Text using XPath in Selenium test
It seems text is wrapped inside a label and not input. Try this
driver.findElement(By.xpath(".//label[text()[normalize-space() = 'Own Hotel']]"));
There is nice explanation about this xpath pattern here
Selenium and Java: How do I get all of the text after a WebElement
I have specified three approaches to extract the text from the result box. Please check all the approaches and use the required approach.
If you want to extract all the text, then you can find the element of the result box and then you can get the Text from that.
WebElement result=driver.findElement(By.id("mid"));
System.out.println(result.getText());If you want to extract the Text based on the Section by section, then you can go with the below approach,
List<WebElement> sectionList=driver.findElements(By.xpath("//div[@id='mid']/div"));
int i=0;
for(WebElement element:sectionList){
System.out.println("Section "+i+":"+element.getText());
i++;
}If you want to extract the text from specific section, then you can do with the below approach
List<WebElement> sectionList=driver.findElements(By.xpath("//div[@id='mid']/div"));
int i=0;
//Inorder to get the Section 3 Content
int section=2;
for(WebElement element:sectionList){
if(section==i){
System.out.println("Section "+i+":"+element.getText());
}
i++;
}
Edit: To address followup question
I would suggest to use some explicit wait after doing some action which resulting in some element rendering. In your code, after doing some modification, I am getting the result as expected.
- In
openTestSite
method, I have just added the explicit wait to ensure the page load after loading the URL - In
enter
method, actually you are getting the autocomplete suggestion after entering the query value .So, we need to just select the value from the autocomplete. - In
getText
method, Search result is taking more time.So, we need to add some explicit wait using any one of the dynamically loading element locator.
Code:
openTestSite Method:
public void openTestSite() {
//driver.navigate().to(the URL for the website);
driver.get("https://wonef.fr/try/");
driver.manage().window().maximize();
//Explicit wait is added after the Page load
WebDriverWait wait=new WebDriverWait(driver,20);
wait.until(ExpectedConditions.titleContains("WoNeF"));
}
enter Method:
public void enter(String word) {
WebElement query_editbox =
driver.findElement(By.id("query"));
query_editbox.sendKeys(word);
//AutoComplete is happening even after sending the Enter Key.
// So, Value needs to be selected from the autocomplete
WebDriverWait wait=new WebDriverWait(driver,20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='autocomplete']/div")));
List<WebElement> matchedList=driver.findElements(By.xpath("//div[@class='autocomplete']/div"));
System.out.println(matchedList.size());
for(WebElement element : matchedList){
if(element.getText().equalsIgnoreCase(word)){
element.click();
}
}
//query_editbox.sendKeys(Keys.RETURN);
}
getText Method:
public void getText() {
WebDriverWait wait=new WebDriverWait(driver,20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='mid']/div")));
WebElement result=driver.findElement(By.id("mid"));
System.out.println(result.getText());
}
I have tested with the above modified code and it is working fine.
Related Topics
Setting Up Auto Compile for Stylus
Overriding :Root CSS Variables from Inner Scopes
How to Align Jsf Components to Center
Translate VS Transform-Origin CSS3
How to Select First Img Tag in a Div with Many Img Tag
Flexbox Space-Between and Align Right
Flexbox - Align-Self: Flex-End Horizontally
How to Get a Floating-Div to Fill Available Space Within Its Parent Div
How to Style a List of Checkboxes
Changing Font Color of <A> Contained Within <Li>, But on Hover Over <Li>
Bootstrap - Some Kind of Margin on Mobile Devices (Only Right Side)
Possible to Build an Svg That Has Fluid Horizontal Scaling, Similar to Old Table-Slice Method