Unable to click on Chinese linkText on Salesforce Chinese version - java

All I am trying to go to Chinese sales force . and click on the products linkbyText using UTF. How do I do it? When I do via my method I get an exception.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.salesforce.com/cn/");
driver.findElement(By.linkText("\ufeff\u89e3\u51b3\u65b9\u6848")).click();
System.out.println("finish");
Error Message:
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to locate element: {"method":"link text","selector":"解决方案"}
Command duration or timeout: 1.08 seconds For documentation on this
error, please visit:
http://seleniumhq.org/exceptions/no_such_element.html

It works perfectly fine if I use Chinese characters directly in the string.
driver.findElement(By.linkText("解决方案")).click();
Or use XPath:
driver.findElement(By.xpath(".//a[text()='解决方案']")).click();
However, the best would be matching href attribute instead of text.
driver.findElement(By.xpath(".//a[#href='/cn/solutions/']")).click(); // XPath version
driver.findElement(By.cssSelector("a[href='/cn/solutions/']")).click(); // CSS selector version

Its a common misconception that linktext method used href value to look for an element. Rather it needs just the text part of the link that is visible which in your case is chinese text.
driver.findElement(By.linkText("解决方案")).click();
In case some part of it is varible, you can use By.PartialLinkText method passing the constant part of the link text as parameter

Related

Unable to locate elements on a website , i tried all the locators but I get the error "No such element" . I am a self taught beginner to Automation

Element HTML:
Inbox
What I tried:
driver.findElement(By.cssSelector("a[text='Log out']"));
then
driver.findElement(By.xpath("//a[.='Log out']"));
Element snapshot:
HTML
driver.findElement(By.linkText("Log out"));
Something like that should work, you should provide the page html for a better response.
My response is based on the fact that in your first try you are saying text='Log out'.
findElement in selenium works with locatorStrategies (By.cssSelector/By.linkText...) the one that i used (linkText) search in all the anchor tags that are found in the pages () and analyze the text inside the tag, if the text is matched the driver will find the element.
Let me know if it works, otherwise provide me the web page html snippet.
I've seen the actual screen, you must change Log out with Inbox
driver.findElement(By.linkText("Inbox"));
Given the HTML:
Inbox
You need to take care of a couple of things here as follows:
Within cssSelector doesn't supports the :contains("text") method
Within xpath for exact text matches you need to use text()
Solution
To identify the element you can use either of the following locator strategies:
Using linkText:
WebElement element = driver.findElement(By.linkText("Log out"));
Using cssSelector:
WebElement element = driver.findElement(By.cssSelector("a[href$='INBOX'][title='View the Inbox']"));
Using xpath:
WebElement element = driver.findElement(By.xpath("//a[text()='Inbox' and #title='View the Inbox']"));

How to click on image/button using java selenium chromedriver

I would like to clik one button, as of now I'm using a XPATH, but sometime the page is changed and I've to update the XPATH.
I'm checking a different way (search and click a image like play.png, click by text, etc.), in order to obtain same result.
Following the portion of the HTML code:
<button class="btn btn-green false">PLAY</button>
EDIT:
I've tried with the code
driver.findElement(By.xpath("//*[contains(text(),’PLAY’]")).click();
but it goes in exception
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //*[contains(text(),’PLAY’] (tried for 10 second(s) with 500 milliseconds interval)
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
at seleniumTest.TestSelenium.play(TestSelenium.java:300)
at seleniumTest.TestSelenium.open_url(TestSelenium.java:277)
at seleniumTest.TestSelenium.seleniumTest(TestSelenium.java:108)
at seleniumTest.TestSelenium.main(TestSelenium.java:85)
Caused by: org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //*[contains(text(),’PLAY’] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[contains(text(),’PLAY’]' is not a valid XPath expression.
You can also use contains or starts-with() when the values of the attributes are dynamic i.e. changing like:
//*[contains(text(),’PLAY’)]
You have to find something that doesn't change.
For example you could do that in XPath using wildcard:
//button[text()='PLAY']
Also you could use the other ways to identify elements in Selenium.
For example By.id(...) if your elements have a unique id.
See Selenium doc for class By

Selenium WebDriver to match similar xpaths

I have following xpaths, which should be handled on same way by WebDriver, I need to get text content from these.
//*[#id="dialogMessage"]/div[3]
//*[#id="dialogMessage"]/div[3]/p
//*[#id="dialogMessage"]/div[3]/p/span[2]
I tried to use this code to match all of the above ones.
String result_text = driver.findElement(By.xpath("//*[contains(#value, 'dialogMessage')]")).getText();
Only one of these xpaths is present on page in each page loads. I get following error message:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//id[contains(#value, 'dialogMessage')]"}
Looks like you're trying to get all elements that contain attribute value equals to dialogMessage instead try using this xpath:
//*[contains(#id, 'dialogMessage')]
or
//*[#id='dialogMessage']

How to verify an invisible element on the Web Page

I am testing a link using webdriver, but i am not able to locate where that link(element) is there on the webpage. How to detect that where the given element is present on the webpage?
Can anybody help?
From the Getting Started Documentation, you can use:
WebElement element = driver.findElement(By.name("elementName"));
Where elementName is the name of the element that you are looking for.
Use Ctrl+Shift+I in chrome browser, and try to find that element in the developer's console using Ctrl+F. (You will have to click developer console's screen first before you do Ctrl+F)
U can check the presence of element by using explicit wait
wait.until(ExpectedConditions.visibilityOf(element))
it will return true or false, boolean value can be used.

How to Manipulate Search Bar That's Not Present in HTML Source Using Selenium?

I'm trying to use Selenium (Java) to automate some searches.
When I go to the page and click Inspect Element on the search bar, I see that it has the id searchBox and name q, both of which are potentially useful. Nevertheless, these properties are nowhere to be found in the HTML when I go to View Source.
When I try to use
WebElement search = driver.findElement(By.id("searchBox"));
or
WebElement search = driver.findElement(By.name("q"));
both come back as unable to be found.
How do I proceed with populating the search field then hitting submit (the submit button also is missing in the source page) if I can't find either of these elements?
EDIT:
As requested, here's a more detailed picture of what's wrong:
The URL of the page accessed by WebDriver is http://www.ycharts.com using the line
driver.get("http://www.ycharts.com/login");
If you go to this page with your actual web browser, right click on the search bar and choose Inspect Element, you'll see that its ID is "searchBox" and its name is "q". However, if you go to view the same page's source, you'll see that there's no such element in the HTML. Clearly, this is why WebDriver can't find it.
driver was initiated as follows:
WebDriver driver = new HtmlUnitDriver();
When I try something like
WebElement search = driver.findElement(By.id("searchBox"));`
The exception returned is:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element with ID: searchBox
So, back to the original question: clearly, the element is there, but it's not in the HTML – how do you interact with it?
The problem is being caused by the fact that the search box is added to the html post page load by javascript.
The HTML returned by http://ycharts.com/ does not contain the searchbox, therefore when Selenium thinks the page has finished loading (i.e DOM ready state), there is no search box.
In order to interact with the search box, you need to tell Selenium to wait until the box appears in the DOM.
The easiest way to achieve this is with WebDriverWait;
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));

Categories