Please check out the element of this website.
It has a form, and along with 2 text input and 1 submit button.
I dont know which one from those 2 inputs that is actually used when the user type-in some urls over there.
But when I tried this (using firefoxDriver) to get the element:
WebElement textfieldURL = driver.findElement(By.id("ping-url")); // even ping-box not working
The result's unable to locate the element.
Then I change my code to this :
driver.switchTo().frame(driver.findElement(By.className("ping-iframe")));
WebElement textfieldURL = driver.findElement(By.id("ping-url")); // even ping-box not working
The result's still unable to locate the element.
Any clues?
You haven't mentioned the exception which you are facing. As your input tag present under iframe so you need to first switch into frame and than have to perform actions -
driver.switchTo().frame(driver.findElement(By.className("ping-iframe")));
//or you can use frame index as well
driver.switchTo().frame(0);
your element is available with the id ping-box . Try the following complete code -
System.setProperty("webdriver.gecko.driver","D:/Application/geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.twingly.com/ping");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.switchTo().frame(driver.findElement(By.className("ping-iframe")));
driver.findElement(By.id("ping-box")).sendKeys("http://www.google.com");
driver.findElement(By.id("ping-button")).click();
Same is working for me.
Related
I am using this code, but it's not working but xpath in the picture is correct. I am not allowed to use (//h2//a[#class='question-link'])[2] because I need to use previous webelement:
driver.get("https://stackexchange.com/");
WebElement e = driver.findElement(By.xpath("//h2//a[#class='question-link']"));
WebElement f=e.findElement(By.xpath(".[2]"));
System.out.println(f.getText());
The second element you want is just another element that is located by the first locator. The easiest way to do what you are asking is to change the first statement to use .findElements() (plural) and then get the second element returned.
List<WebElement> e = driver.findElements(By.xpath("//h2//a[#class='question-link']"));
System.out.println(e.get(1).getText()); // index starts at 0 so 1 is the second element
Instead of again writing Webelement f write the complete xpath including [2] in Webelement e itself.
Here is an example in Python using Selenium to extract the text from the second link using a previous WebElement:
from selenium import webdriver
initialize the browser
driver = webdriver.Firefox()
navigate to the website
driver.get("https://www.example.com")
locate the previous WebElement
previous_element = driver.find_element_by_css_selector("#element_id")
locate the second link relative to the previous WebElement
second_link = previous_element.find_elements_by_tag_name("a")[1]
extract the text from the second link
text = second_link.text
close the browser
driver.quit()
Note: Replace #element_id with the actual id of the previous WebElement and make sure to install the Selenium library if not already installed.
I am doing automation using selenium webdriver (java) on a search engine BookMyCrop (http://www.bookmycrop.com). Here, I searched for a crop but, I am not able to click on desired search result. Please help me with it.
Code :
WebElement search = driver.findElement(By.xpath("//*[#id=\"search_keyword\"]"));
search.sendKeys("59825");
search.sendKeys(Keys.ENTER);
driver.findElement(By.partialLinkText("Cashew")).click();
------My 1st try-------------
//WebElement link = driver.findElement(By.xpath("\"//div[#id = 'Links']/a[3]\""));
//link.click();
------My 2nd try-------------
//List<WebElement> find = driver.findElements(By.xpath("/html/body/section[2]/div[2]/div/div/div/div[1]"));
//find.get(1).click();
}
} –
You can use the css selector based on class names: ".product-block.inner-product-block" and get the list of all the search results.
Then click on whatever index you want to click.
I am not using an IDE for this but it would look something like this:
driver.findElements(By.cssSelector(".product-block.inner-product-block")).get(0).click();
As said, you can try with css ".product-block.inner-product-block"
Then
get List of WebElements
do loop
inside loop, try get text of each element or innerText attribute
cross check if it is required one or not by simple if condition
If so, click on that web element and break loop
if this locator is not giving required info, try other locator. say $$("a h3") for veg names.
The below code worked for me. It is navigates to the correct link
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://www.bookmycrop.com");
WebElement search = driver.findElement(By.xpath("//*[#id=\"search_keyword\"]"));
search.sendKeys("59825");
search.sendKeys(Keys.ENTER);
driver.findElement(By.partialLinkText("Cashew")).click();
I have tried a different method to input elements like
Here is xpath I used
By NameOfTheProperty=By.xpath("//fieldset/div/input[#name='name']");
By NameOfTheProperty=By.xpath("//div/input[#name='name']");
By NameOfTheProperty=By.xpath("//input[#name='name']");
Tried with Selenium Builder
WebElement element=driver.findElement(by);
Actions builder = new Actions(driver);
Action mouseOverHome = builder
.moveToElement(element)
.click().sendKeys(text).build();
mouseOverHome.perform();
Tried with
WebElement element=driver.findElement(by);
element.sendKeys(text);
None of the methods is working..I can not able to input text inside the field and It shows
Element not interactable
Here is the site
I hope someone help me to find out the solution..
Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
xpath you should checks is :
//input[#name='name']
if it is unique, then you can use Javascript executor :
WebElement password_input = driver.findElemenet(By.xpath("//input[#name='name']"));
((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('value', 'password_should_be_written_here')", password_input)
personally I would use id's if the element has one.
"//*[#id='__BVID__104']"
Make sure you are waiting for the element to be ready.
WebDriverWait wait = new WebDriverWait();
wait.until(ExpectedConditions.elementToBeClickable(element));
If it then times out on wait.until(... then I've found that sometimes an element must first be clicked for it to be exposed.
If this is the case. You will have to inspect the element before it is clicked. Find it's xpath, and see if it changes when it is clicked. If so then create a webElement for that element and have Selenium first click it, before clicking the actual field/element.
I am using firebug for finding xpath, this is the error that is displayed in the error console.
no such element: Unable to locate
element:{"method":"xpath","selector":".//*[#id='select2-contact_id-result-v0w5-258']"}
driver.findElement(By.xpath(".//*[#id='select2-contact_id-result-v0w5-258']")).click();
html is as follow
id="select2-contact_id-result-v0w5-258" class="select2-results__option select2-results__option--highlighted" role="treeitem" aria-selected="false">Single contact
There could be following reason for it :-
May be your id is dynamically generated, so you need to try with different locator to create By object as below
By by = By.className("select2-results__option select2-results__option--highlighted");
or
By by = By.cssSelector(".select2-results__option select2-results__option--highlighted");
or
By by = By.xpath("//*[contains(., 'Single contact')]");
or if id is not dynamically generated
By by = By.id("select2-contact_id-result-v0w5-258");
May be your element is inside a frame or iframe, If it is then you need to switch that frame or iframe before finding element as below :-
driver.switchTo().frame("frame id or name or index");
May be element is not being fully loaded on the page due to slow internet, so you need to implement WebDriverWait to wait until element is visible and clickable as below :-
WebDriverWait wait = new WebDriverWait (driver, 10);
WebElement el = wait.until(ExpectedConditions.elementToBeClickable(by)); //use anyone of the above by object
Now after successfully getting element you need to perform click as below :-
el.click();
Note :- if your element is actually a dropdown with select tag, then you need to create Select() object to work with dropdown as below :-
Select sel = new Select(el);
//now perform step to select an option by visible text from dropdown
sel.selectByVisibleText("your visible option text");
or
sel.selectByIndex("your option index");
or
sel.selectByValue("your option value");
Hope it helps you..:)
I am using Java and Firefox and Firebug
I am not able to locate the element on the second page. For example if I login to gmail then I am not able to locate and click on the sent items or any other button on the next page.
I tried with the xpath (both absolute and relative) but every time I am getting an error that element not found.
with the code I am successfully able to login but as soon as the page loads I get an error "Element not Found".
Please suggest any solution
Unless you are telling WebDriver to wait until the element on the 2nd page is loaded, WebDriver will simply try to click the element as soon as its able to run. This is bad because your element might not yet be loaded while WebDriver is already trying to click it... TIMEOUT mayhem ensues...
Try the following... use the WebDriverWait class to make WebDriver wait for the element on the page to be loaded before attempting to click it...:
WebDriverWait wait = new WebDriverWait(driver, 100);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("your xpath")));
element.click();
The '100' in WebDriverWait(driver, 100) is the maximum amount of seconds you want WebDriver to repeatedly attempt to locate the element before it times out...
I agree with the answer by CODEBLACK. Also you can go for Imlicit wait,which would make selenium wait implicitly for a given period of time.
Try following:-
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
You can specify time as per your convenience.
Best O Luck. . .!