Selenium webdriver (java) - Click on desired link of some search results - java

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();

Related

How to extract text from second link using previous webelement?

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.

Selenium not able to input element in the form inside fieldset tag

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.

Not able to select any element from chosen dropdown having hidden attribute using testNG Selenium 3.3.1 in java

I have tried different ways, but not able to get any success. Please help me to find some solution for this problem.
I am testing an application having a page like this.
Please refer to this page and help me to select values from dropdown given above page.
BTW with the help of following lines, I am able to click on dropdown, but then not able to select any value using different techniques.
WebElement source = driver.findElement(By.cssSelector("#step_language > div.well.well-lg > div > div:nth-child(2) > div > div.mars_chosen_container.clearfix"));
source.click();
Try this way.
driver.get("https://www.marstranslation.com/place-order");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//a/span[contains(text(), 'English')]")).click(); //Click on dropdown using xpath locator.
WebDriverWait wait = new WebDriverWait(driver, 15); //Use explicit wait method for find an element
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//ul/li[contains(text(), 'Arabic')]"))));
driver.findElement(By.xpath("//ul/li[contains(text(), 'Arabic')]")).click(); //After explicit wait, click on Arabic option from dropdown using xpath locator.
Try this snippet
WebElement TargetLanguage = driver.findElement(By.cssSelector("#targetLanguageId_chosen>ul>li>input"));
TargetLanguage.click();
Thread.sleep(3000);
// Gets the target languages in the List
List<WebElement> LangElements = driver.findElements(By.cssSelector("#targetLanguageId_chosen>.chosen-drop>ul>li"));
for(WebElement t : LangElements)
{
if(t.getText().equalsIgnoreCase("Arabic"))
{
t.click();
}
}
Here as an example I clicked on Arabic. You can replace the same with your required language and use it.
Hope it works for you. If anything goes wrong please feel free to ask.
I prefer to write functions for things like this since they will likely be reused.
public static void SelectSourceLanguage(String language)
{
driver.findElement(By.cssSelector("a.chosen-single")).click(); // click the dropdown
driver.findElement(By.xpath("//ul[#class='chosen-results']/li[contains(.,'" + language + "')]")).click(); // click the language
}
Then you can call it like
driver.get("https://www.marstranslation.com/place-order");
SelectSourceLanguage("Hindi");

Selenium getting text input of Twingly (Java Code)

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.

Selenium cssSelector works in IDE but not in Webdriver

I have a web system I am automating using Java/Selenium Webdriver. I have an item I am trying to get access to. It has a compound class name. I have tried all the solutions I have been able to find here and so far none of them work.
The most offered solution looks like this:
By elem = By.cssSelector("div.prdbox.saleshdr");
List<WebElement> elements = driver.findElements(elem);
System.out.println("Number of Items found: "+elements.size());
When I check the size of the elements array it is always zero.
What I am finding however is that when I put the selector string in to the Selenium IDE (2.9.1) and use the "Find" button it identifies the correct web element without any problem at all.
I am at a loss for why it works in the IDE but not in my code.
Try selecting the element using its XPath? In the past when I ran into issues trying to select something using cssSelector, I often had success when I tried its XPath instead.
Give some wait time before the selector you are taking.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
List<WebElement> elements = driver.findElements(By.cssSelector("div.prdbox.saleshdr"));
System.out.println("Number of Items found: "+elements.size());
or try to find the elements with the help of Xpath or id.
List<WebElement> elements = driver.findElements(By.xpath("your xpath"));
Hope it will help you

Categories

Resources