I want to print the submenu text of mainmenu of first list[Electronics] of
in selenium webdriver.
url : https://www.flipkart.com
But there is some issue to take the xpath of that sumMenu.
How can I take the xpath and all.
You can try with the following x-path to get all sub menus of main menu "Electronics"
//span[.='Electronics']/following-sibling::ul//li/a
Try the bellow code. Change value the String searchSubMenu = "Electronics"; if you want get text other sub menu, hope this helps.
driver.get("https://www.flipkart.com/");
//wait login popup and click
new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#class='_2AkmmA _29YdH8']")));
driver.findElement(By.xpath("//*[#class='_2AkmmA _29YdH8']")).click();
String searchSubMenu = "Electronics";
int totalSubMenu = driver.findElements(By.xpath("//*[contains(#class,'Wbt_B2')]")).size();
System.out.println("Search for : " +searchSubMenu);
for(int i=1; i<=totalSubMenu; i++) {
String getTextSubMenu = driver.findElement(By.xpath("(//*[contains(#class,'Wbt_B2')])[" +i +"]")).getText();
System.out.println("Get Sub Menu Title : "+ getTextSubMenu);
if (getTextSubMenu.equals(searchSubMenu)) {
driver.findElement(By.xpath("(//*[contains(#class,'Wbt_B2')])[" +i +"]")).click();
Thread.sleep(1000);
String targetAllGetText = driver.findElement(By.xpath("(//*[contains(#class,'_3GtRpC')])[" +i +"]")).getText();
System.out.println(targetAllGetText);
break;
}
}
driver.quit();
It will help you : Please try
String SubMenu = driver.findElement(By.xpath("Xpath of element")).getText();
System.out.println(SubMenu);
if you want Size:
int Size =driver.findElement(By.xpath("Xpath of element"));
System.out.println(Size);
Related
I want to read canvas element value ("Answer:5015") but attribute value does not exist. Can you please let me know how to read attribute value ?
App URL : https://the-internet.herokuapp.com/challenging_dom#edit
This code works, using Java and Edge browser:
System.setProperty("webdriver.edge.driver", "msedgedriver.exe");
WebDriver driver = new EdgeDriver();
driver.get("https://the-internet.herokuapp.com/challenging_dom#edit");
String answer = new String();
ArrayList<WebElement>scripts = new ArrayList<WebElement>((ArrayList<WebElement>) driver.findElements(By.tagName("script")));
for(int i = 0; i < scripts.size(); i++) {
String focusText = scripts.get(i).getAttribute("innerHTML");
if(focusText.contains("canvas.strokeText")) {
answer = focusText.substring(focusText.indexOf("Answer"), focusText.indexOf("',"));
break;
}
}
System.out.println(answer);
I am getting this error when I try to click a date on calendar widget using selenium:
[JavaScript Error: "b.elementFromPoint is not a function" {file: "file:///tmp/anonymous3738058585112632092webdriver-profile/extensions/fxdriver#googlecode.com/components/synthetic-mouse.js" line: 11130}]'[JavaScript Error: "b.elementFromPoint is not a function" {file: "file:///tmp/anonymous373805858
public void clickOnDate() {
String minDate = "12";
webDriverWait(2);
List<WebElement> list = element(test).findElements(By.tagName("td"));
for(WebElement col : list){
System.out.println(col.getText());
if(col.getText().trim().equals(minDate)){
col.click();
}
}
}
Here test is containing
By test = By.className("bs-datepicker-body");
I want to write xpath for the following div:
<div class='someclass' id='someid'>:TEST SELENIUM 1234<div>
Please note :
tag can be anything such as div, span ,or anchor tag.
can be present anywhere in the text.
What I have tried so far :
//div[contains(text(),":TEST SELENIUM 1234")]
//div[contains(text(),":TEST{ }SELENIUM{ }1234")]
//div[contains(text(),":TEST SELENIUM 1234")]
//div[normalize-space(text()) = ':TEST SELENIUM 1234']
//div[normalize-space(text()) = ':TEST{ }SELENIUM{ }1234']
//div[normalize-space(text()) = ':TEST SELENIUM 1234']
//div[normalize-space(.) = ':TEST SELENIUM 1234']
//div[normalize-space(.) = ':TEST{ }SELENIUM{ }1234']
//div[normalize-space(.) = ':TEST SELENIUM 1234']
//div[normalize-space(.) = ':TEST{\u00a0}SELENIUM{\u00a0}1234']
//div[normalize-space(.) = ':TEST${nbsp}SELENIUM${nbsp}1234']
What has worked for me (thanks to #Andersson)
//div[starts-with(text(), ":TEST") and substring(text(), 7)="SELENIUM" and substring(text(), 16)="1234"]
This is more of a work around and would work only for known Strings.
These are the SO post which I have already followed :
Link1
Link2
Any help will be highly appreciated.
According to the conversation above, you can use this sample xPath builder(JAVA):
public class Test {
public static void main(String[] args) {
String s = ":TEST SELENIUM 1234";
String[] parts = s.split(" ");
StringBuilder xpath = new StringBuilder("//*");
for (int i = 0; i < parts.length; i++){
xpath.append((i == 0) ? "[contains(text(), '" + parts[i] + "')" : " and contains(text(), '" + parts[i] + "')");
}
xpath.append("]");
System.out.println(xpath);
}
}
Output:
//*[contains(text(), ':TEST') and contains(text(), 'SELENIUM') and contains(text(), '1234')]
In Python I would do
required_div = [div for div in driver.find_elements_by_xpath('//div') if div.text == ':TEST SELENIUM 1234'][0]
to find required node by its complete text content ignoring non-breaking space chars
P.S. Again it's just a workaround, but it seem to be quite simple solution
How can I get the all element id's of a screen in selenium?
Please refer to this screenshot
Element ID is getting changed every time the page loads. I used contains#id , start-with#id, but it doesn't work every time. Now I want to get all the element id's from the webpage, so I can select the exact element.
My webpage contains input text, buttons, drop-downs.
Use Xpath instead or cssSelector. If you persist on knowing all page ids as a list of String in java, try to execute a javascript function.
// The Firefox driver supports javascript
WebDriver driver = new FirefoxDriver();
// Go to the Google Suggest home page
driver.get("http://www.google.com/webhp?complete=1&hl=en");
ArrayList ids = (ArrayList)((JavascriptExecutor) driver).executeScript(
" var allElements = document.getElementsByTagName('*'); var allIds = []; "
+ " for (var i = 0, n = allElements.length; i < n; ++i) { "
+ " var el = allElements[i]; "
+ " if (el.id) { allIds.push(el.id); } } return allIds; "
);
Regards,
Alan Mehio
London, UK
String strPrimaryNav = "MEN";
String strSecondaryNav = "Shoes";
String strTertiaryNav = "Golf";
driver.findElement(By.linkText(strPrimaryNav)).click();
WebElement weSecNav = driver.findElement(By.className("secondaryButton").linkText(strSecondaryNav));
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseDown(((Locatable)weSecNav).getCoordinates());
//just trying with for loop as the tertiary popup disappears quickly and hence getting ElementNotVisible Exception
for (int i = 0 ; i< 2; i++){
mouse.mouseDown(((Locatable)weSecNav).getCoordinates());
//mouse.mouseMove(((Locatable)weSecNav).getCoordinates(), 0, 0 );
//WebElement weTerNav = driver.findElement(By.className("tertiaryButton").linkText(strTertiaryNav));
WebElement weTerNav = driver.findElement(By.linkText(strTertiaryNav));
boolean isSecDisplayed = ((RenderedWebElement)weTerNav).isDisplayed();
System.out.println("isDisplayed: " + isSecDisplayed);
System.out.println(" " + ((RenderedWebElement)weSecNav).getAttribute("href"));
System.out.println(" " + ((RenderedWebElement)weSecNav).getValueOfCssProperty("action"));
weTerNav.click();
}
I was trying the below code using selenium 2 but, the tertiary popup not stays long to click it and hence getting ElementNotVisible exception at Tertiary click.
You can at least check that the element is visible before you send your click:
Selenium s = new WebDriverBackedSelenium( driver, URL );
s.waitForCondition( "!( document.getElementById('.....') === null )", "20000" );
s.click( .... );
This avoids the exception. I'm not sure there is a way to make the popup stay any longer than it should.