I am trying to get the text for Rating of any product on Amazon but I am unable to write the correct code. I don't understand what I am doing wrong here. Here its not even able to locate the element.
Also I don't think xpath is wrong because I checked with Firepath.
Below is the code:
public static void main(String args[])
{
System.setProperty("webdriver.gecko.driver", "D:\\Eclipse and workspace\\eclipse\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amazon.in/");
WebElement elem = driver.findElement(By.id("twotabsearchtextbox"));
elem.sendKeys("Camera DSLR");
driver.findElement(By.className("nav-input")).click();
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='result_0']/div/div/div/div[2]/div[3]/div[2]/div[1]/span/span/a/i[1]/span")));
WebElement elem2 = driver.findElement(By.xpath(".//*[#id='result_0']/div/div/div/div[2]/div[3]/div[2]/div[1]/span/span/a/i[1]/span"));
elem2.getText();
}
Please help me out.
The span element you want to get contains text like "4.4 out of 5 stars", but actually you see just an icon with the stars, so it's bad idea to use visibilityOfElementLocated condition as it won't be visible anyway.
Try to use presenceOfElementLocated instead:
WebElement elem2 = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[#class='a-popover-trigger a-declarative']//span[#class='a-icon-alt']")));
elem2.getAttribute("textContent");
Note that to get text content of invisible span you should use getAttribute("textContent") instead of getText()
Use dynamic xpath instead of absolute xpath:
By.xpath("//*[contains(#class,'a-icon-star')]//*[contains(#class,'a-icon-alt')]")
And as rightly pointed out by #Andersson,
Use presenceOfElementLocated instead of visibilityOfElementLocated as tooltips are designed to be invisible.
Use textContent attribute for invisible elements such as tooltips instead of getText().
Related
I have trouble identifying 'sign in with Apple ID' element (at iclod.com page).
This is what I'm using now:
WebElement username = driver.findElement(By.xpath("//[#id=\"account_name_text_field\"]");
username.sendKeys("my_email#icloud.com");
Also, I tried to use CSS created by Chropath and Ranorex, still not working.
What I'm doing wrong?
Path to needed Element
Please check below solution. Iframe is associated with your web page and you need to switch to iframe before you interact with web element input box.
driver = webdriver.Chrome(executable_path=r" path of chromedriver.exe")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
driver.get("https://www.icloud.com")
wait.until(EC.presence_of_element_located((By.ID, "auth-frame")))
driver.switch_to.frame("auth-frame")
inputBox = wait.until(EC.element_to_be_clickable((By.ID, "account_name_text_field")))
inputBox.send_keys("your test")
# switch back to main window
driver.switch_to.default_content()
output:
The element is inside the iframe, you need switch first.
You can use .frameToBeAvailableAndSwitchToIt:
driver.get("https://www.icloud.com/");
new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("auth-frame")));
WebElement username = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("account_name_text_field")));
username.sendKeys("my_email#icloud.com");
Add WebDriverWait, the web loading is rather long.
Iframes can be tricky. You have to change focus to the frame before accessing that text box.
Use switchTo().frame ();
It can take the following parameters :
switchTo().frame(int frame number): Defining the frame index number,
the Driver will switch to that specific frame
switchTo().frame(string
frameNameOrId): Defining the frame element or Id, the Driver will
switch to that specific frame
switchTo().frame(WebElement
frameElement): Defining the frame web element, the Driver will
switch to that specific frame
WebDriver driver = new FirefoxDriver();
waitdriver = new WebDriverWait(driver , 10);
driver.get("https://www.icloud.com/");
driver.switchTo().frame("auth-frame");
waitdriver.until(ExpectedConditions.presenceOfElementLocated( By.xpath("//*[#id=\"account_name_text_field\"]")));
WebElement textbox=driver.findElement(By.xpath("//*[#id=\"account_name_text_field\"]"));
textbox.click();
textbox.sendKeys("HelloWorld#gmail.com");
1) The xpath pattern which you have written is wrong.
2) You can always verify the xpath or cssSelector in ChroPath before using it in your script.
3) If the element is inside iframe then you must have got the iframe xpath as well as the element xpath in ChroPath. Please use them.
Please follow this video tutorial to make the best use of this iframe feature of ChroPath.
Well I have below code
<button class="jobs-search-box__submit-button artdeco-button artdeco-button--3 ml2" data-ember-action="" data-ember-action-689="689">Search</button>
I want to find this element in selenium and perform click action. I tried several options like by class, xpath, name, text, contains but nothing worked.
Can someone guide me here?
driver.findElement(By.xpath("//button[contains(.,'Search']")).click();
driver.findElement(By.className("jobs-search-box__submit-button artdeco-button artdeco-button--3 ml2")).click();
driver.findElement(By.className("//*[#id=\"ember689\"]/button")).click();
driver.findElement(By.linkText("Search")).click();
To summarize what was in the comments. Each locator had something off.
By.xpath("//button[contains(.,'Search']")
was missing a parenthesis and needed to be:
By.xpath("//button[contains(.,'Search')]")
Meanwhile, because By.className expects a single className
By.className("jobs-search-box__submit-button artdeco-button artdeco-button--3 ml2")
also does not work. (see github.com/seleniumhq/selenium/issues/1480
but could as:
By.cssSelector(".jobs-search-box__submit-button.artdeco-button.artdeco-button--3.ml2")
Also
By.className("//*[#id=\"ember689\"]/button")
refers to an id not presented (Also, I'm not sure, but I think would need to be by xpath).
By.linkText("Search")
does not work because there is no tag a and so no hyperlink.
In Protractor this is much simpler because you would just say by.buttonText('Search')
You can achieve the same things by using javascript. kindly find the below example of code:
//Creating the JavascriptExecutor interface object by Typecasting
JavascriptExecutor js = (JavascriptExecutor)driver;
WebElement button =driver.findElement(By.xpath("//button[#class='jobs-search-box__submit-button artdeco-button artdeco-button--3 ml2']"));
//Perform Click on LOGIN button using JavascriptExecutor
js.executeScript("arguments[0].click();", button);
I hope it will work on your case.
Note: Make sure your element will be static.
The correct XPath locator would be:
//button[text()='Search']
If you won't be able to locate it using the above query, make sure that:
The button doesn't belong to and <iframe>, if this is the case - you will have to change the context using switchTo() function
The element is present in DOM, i.e. the page has been loaded fully. It's better to use Explicit Wait for element location/interaction like:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Search']")));
More information: How to use Selenium to test web applications using AJAX technology
Try with These two hope it works,
1.) Using Contains
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Search')]")));
2.) Using CSS
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.jobs-search-box__submit-button artdeco-button artdeco-button--3 ml2")));
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.contains('Search')")));
If does not work let me know i'll provide another Solution.
I am getting a very long xpath for an element that I selected. Is there anyway to shorten it? This is the xpath I am getting:
//li[#class='menu_men 1-level hasChild']//div[contains(#class,'level-2')]//div[#class='menu-wrapper']//ul[#class='level-2']//li[#class='1-level']//div[#class='level-3']//ul[#class='level-3']//li//a[#class='level-3'][contains(text(),'Socks')]
This is the URL: Calvin Klein Singapore I hovered over 'MEN', the accessories section will appear, than I hover the 'Socks' to get the xPath.
I am getting the following execption in my code and I am wondering if somehow the long xpath could be one of the reasons:
org.openqa.selenium.NoSuchElementException: no such element: Unable to
locate element: {"method":"xpath","selector":"//li[#class='first
menu_men 1-level
hasChild']//div[contains(#class,'level-2')]//div[#class='menu-wrapper']//ul[#class='level-2']//li[#class='1-level']//div[#class='level-3']//ul[#class='level-3']//li//a[#class='level-3'][contains(text(),'Socks')]"}
I am using cropath from within chrome developer tools to get the xPath.
I am new to automation, I really hope someone can advise. Thank you.
#SameerArora this is the code I have to clear the pop up window, as what I had mentioned in the comments below.
//for clearing the popup window
#FindBy(how=How.XPATH,using="//*[starts-with(#id,'popup-subcription-closes-link-')]")
public WebElement newsletterpopup;
public String clickCategory(){
//.....
resusableFunctions.buttonClick(driver, newsletterpopup, "popoup");
}
public void buttonClick(WebDriver driver, WebElement element, String elementName) throws InterruptedException
{
try
{
element.click();
System.out.println("Log: ResuableFunction.buttonClick");
}
catch (org.openqa.selenium.ElementNotInteractableException notInteract)
{}
The element you are looking for can be found using xpath:
WebElement element = driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]"));
However, as the element is not visible directly when you are opening the link, you would be getting NoSuchElementException, so to resolve it you can use javascript click method on the element which directly operates on the div of the page.
Addition to this, i can see that a subscription popup comes when i am opening the page for the first time, so you need to dismiss that popup first(if the popup is present) and then click on the "Socks" element using the JavaScript click method.
Your code should be like:
List<WebElement> closeSubscriptionPopUp = driver.findElements(By.xpath("//a[contains(#id,'popup-subcription-closes-link')]"));
if (closeSubscriptionPopUp.size() > 0) {
closeSubscriptionPopUp.get(0).click();
}
WebElement sockElement = driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", sockElement);
To hovered over 'MEN' >> accessories >> 'Socks' section, You need to use selenium Actions class.
As it is not really possible to first click on men(as it will open other section),
So to hover to sock, you need to chain all of the actions that you want to achieve in one go.
Process should be:
move to men element first
Move to accessories
then move to Socks and click on it.
Note: By using Action class, we can chain all the process in one single go.
As mentioned below
1) First way:
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("(//a[contains(text(),'MEN')])[2]")))
.moveToElement(driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]")))
.click().build().perform();
2) Second way with wait:
WebDriverWait wait= new WebDriverWait(driver, 10);
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("(//a[contains(text(),'MEN')])[2]"))).build().perform();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//a[contains(text(),'Socks')])[1]")));
action.moveToElement(driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]")));
action.click().build().perform();
Try this:
//a[normalize-space(text()) = 'Socks']
I would recommend you to not use such long xpath's and try to write xpath on your own.
Try :
//li[contains(#class,'menu_men')]//a[contains(text(),'Socks')]
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.
I am using Selenium WebDriver to get from a drop down list values. Unfortunately I can't get it, because my code can't recognise the xpath.
Here's my code:
WebElement selector = driver.findElement(By.xpath("id('search')/x:fieldset/x:table[1]/x:tbody/x:tr[2]/x:td[1]/x:select"));
Select s = new Select(selector);
List<WebElement> options = s.getOptions();
for (WebElement wb : options) {
System.out.println(wb.getText());
}
The problem is with the 1st line (WebElement selector). In output I get something like this:
Exception in thread "main"
org.openqa.selenium.InvalidSelectorException: The xpath expression
'id('search')/x:fieldset/x:table[1]/x:tbody/x:tr[2]/x:td[1]/x:select'
cannot be evaluated
I've even tried to find by name or class, but selenium still doesn't find this list.
How to solve the problem? Thanks in advance :)
Ganjira, This is no way to write a xpath. If you find it tough to recognize item . Use Selenium IDE 'Select' button.
If you can provide sample of html page on which you try to find your element, it would be very helpful. Anyway, try to search using css selectors e.g.
WebElement selector = driver.findElement(By.css("#search > select"));