I want to search something on Google using Selenium chromedriver and enter it. I can normally do this within the site, but I couldn't type it into google. What code can we use for this?
driver.findElement(By.xpath("//input[#class='desktopOldAutosuggestTheme-UyU36RyhCTcuRs_sXL9b']")).sendKeys("HBCV00000ODHHV");
Fakat olmadı.
The easiest way to do so is via the url when you perform driver.get(...).
Look at the google url for german shepards: https://www.google.com/search?q=german+shepards .
If you want to go the google.com and type in the search box you need to perform a better xpath query then by class name. As mentioned by #f1sh this is because google generates the class names (for scope based css). For me the following works for the search bar.
search_bar : WebElement = driver.get_element(By.XPATH, "//input[#title='Search']")
# then you can perform the send_keys
search_bar.send_keys(...)
Good Luck!
I think the problem is not the search text because I tried too many terms and it didn't work.
driver.findElement(By.xpath("//input[#class='desktopOldAutosuggestTheme-UyU36RyhCTcuRs_sXL9b']")).sendKeys("HBCV00000ODHHV");
driver.findElement(By.xpath("//input[#class='desktopOldAutosuggestTheme-UyU36RyhCTcuRs_sXL9b']")).sendKeys(Keys.ENTER);
these are just an example on different sites. I tried id class type as xpath in every way, but it didn't write the text I wrote in the search bar.
Related
I am trying to find a unique locator of the "update profile picture button" in my Facebook account to use it for automation test (selenium webdriver with java).
I use driver.findElement(By.className("_156p")).click();, but it doesn't work.
What should I use?!
Being unable to see the rest of the html source, I can't say for sure, but my guess is that the class is actually defined earlier in the source. The problem with using just a class name is that class names do not have to be unique on a page. The unique version of a class name is id, and if you are testing code that you can edit, try to use lots of ids.
If that's not the case, a decent way to deal with code that you can'y edit yourself is to use a css selector. Really good information on doing them here.
Another good debugging option is to use javascript or python to run a webdriver from the terminal. Because these languages are not compiled, you can run them in real time, which can allow you to tweak a class name a lot quicker. If you don't have experience with python, check this out. By using python/javascript you can create the webdriver, and then keep typing in driver.find_element_by_whatever("value_to_find") while on the same page. This will be much quicker than running the java program from scratch for every different "value_to_find".
As the page source you have provided is pretty limited unable to come up with a stable locator. How about getting the div using the text through xpath.
"//div[.='Update Profile Picture']"
Try This:
WebElement element = driver.findElement(By.className("_156n"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Worked for me.
Consider:
I need to find a way how I could get the text from the popup using Selenium/core Java, so that I can compare the text with expected data.
Is there a way I could extract the text from the popup?
A screenshot is attached.
If you inspect the element which has the focus.
This is possible with all common browsers via development tools or use the SeleniumIDE Plugin for Firefox to get information about that page and build locators.
Often framework are used (like bootstrap) to ensure a consistent layout so a CSS-locator might look like:
var popupBody = driver.FindElement(By.CssSelector("div.modal-dialog div.content div.body")
Note that in this case Bootstrap would not the call the class 'popup' but 'modal-dialog'. The locator might furthermore vary depending on the inner structure. As mentioned inspect the element (or share the HTML code so we can suggest concrete locator).
By this you get a normal WebElement ala Selenium where you can get the Text
// use the element
.. popupBody.Text ..
I would like to know how to identify via webdriver the following html "node":
thank you <em>very much indeed</em> - Angielsko-Polski Słownik <b>...</b>
(It's just any link of google when one launch a google search)
I have googled it, however I have found only cases where the id or the class were provided.
What about in this case?
This is my failing try:
webdriver.findElement(By.xpath("//a[#href='http://www.google.pl/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCcQFjAA&url=http%3A%2F%2Fpl.bab.la%2Fslownik%2Fangielski-polski%2Fthank-you-very-much-indeed&ei=Sia8U6LPCevB7AagwoCICg&usg=AFQjCNF6y7swYrp3axD0hNrCWfjovhcVPw&bvm=bv.70138588,d.bGE']")).click();
Thanks in advance.
There are several possibilities:
By.tagName("a")
However, chances are there are more than one a tag, and so the above will pick the first one it encounters. To get more specific, you can use:
By.xpath("//a[0]")
0 in this case refers explicitly to the first a tag. However, to give a precise XPath answer, I would need to see more your page code, as well as your exact requirements. You can also use:
By.partialLinkText("thank you very much indeed")
This works best if you have unique enclosed text.
You may also want to read through the rest of the locators in the API.
If you are not able to identify the the link directly, you can try based on other element.
When you have any adjacent div or element having unique value, you can refer the link relative to that.
WebElemenet element = driver.findElement(By.cssSelector("div#id a"));
This will get the link element which is present in the div having an id value of "id".
I'm trying to build an android app for fun, I am able to import the web search box into the app, but what I would like to do, is to have the search restricted to given website(s). So, I would like for the user to be able to enter a string into the text box, and for the search engine to search the given website(s), that is programmed into the script, and return posts associated with the string.
Can someone either explain or reference material the will explain how to do this, or at least give me the name of this process so I could do research myself?
Thank you in advance
If you're using the getStringExtra intent, then what you can do is you can store the search query in a String. Once you get this, you can use HttpGet() where you can specify the website you want to search along with the query.
I haven't done this in Android but if there is no other way in API, you can try to add a prefix to every search query such as "site:siteone.com || site:sitetwo.com " etc. this will force google to search only on siteone.com and sitetwo.com.
But I think there must be a better way.
so I am using HTML Unit to click an item on a webpage. I usually use Xpath to select my items, but this page gives every element a randomly generated ID and class. I usually use Google Chrome to get the Xpath of elements, but it gives me something like this: //*[#id=":og"] where :og is the randomly generated ID. I know that sometimes chrome gives me Xpath without any ID's or Classes, like this: /html/body/table/tbody/tr[2]/td/table/tbody/tr[3]/td/form/table[2]/tbody/tr/td/input[2] Is it possable to get an Xpath that does not rely on IDs or Classes in a case like this?
Thanks.
In order to construct shorter xpaths or alternative ones based on tags only you can use plugins that will let you do just that. Particularly I favor the Selenium IDE in firefox, but in Chrome you can use things like Xpath Helper. There are others you can explore by searching the chrome web store.