Selenium findElement(By.classname  identifier

chima
chima used Ask the Experts™
on
Hello, I
The <input name> DOM line below is similar to one that I have used this Webdriver; driver.findElement(By.className("whsOnd zHQkBf"))
to enter my username.  When I click on the button "Next," the page moves on to the page to enter the password.
This is the line for the password;
<input name="password" tabindex="0" class="whsOnd zHQkBf" dir="ltr" spellcheck="false" aria-label="Enter your password" type="password" jsname="YPqjbf" data-initial-value="" data-initial-dir="ltr" autocomplete="current-password" autocorrect="off" autocapitalize="off">

You'll see that the same class name is used, yet the driver.findElement(By.className("whsOnd zHQkBf")) is not working.
Why?  I don't see any other className or id to use.  What can be used?  The test site is gmail.com
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2015

Commented:
Does it work if you just search on class 'whsOnd'?

Author

Commented:
gurpsbassi, using only half of "whsOnd zHQkBf" is an interesting thoughtt.  I solved the problem by doing a;  driver.get  of the second page.
But then I commented out this line and the line driver.findElement(By.className("whsOnd zHQkBf")).sendKeys(  continues to  work.
If I boot up, I would not be surprised if it stops working.

I'll wait to see if anyone else has any comments, before I will issue the points.
Top Expert 2015

Commented:
I'm not interested in points unless I solve your problem.
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Top Expert 2015

Commented:
Not sure I get what you mean by using 'half'.
It's not half. There are TWO distinct class names separated by a space character

Author

Commented:
gurpsbassi maybe you can offer an explanation why the sequence of events solved the problem, yet the line of code which solved the problem is no longer needed.  I'm afraid the fix will be eliminated if and when I shutdown my computer.
The other question I have is; how does one refresh the browser, i.e., the web page with Webdriver?
Siva Prasanna KumarPrincipal Solutions Architect
Top Expert 2006
Commented:
A CSS class name can't have spaces between them, hence "whsOnd zHQkBf" is invalid CSS class name, if it was a valid CSS class name you wouldn't have encountered this issue. Thanks.

Refer: https://en.bem.info/methodology/naming-convention/
Top Expert 2015

Commented:
@Siva Prasanna Kumar --- there are two classes
Siva Prasanna KumarPrincipal Solutions Architect
Top Expert 2006

Commented:
@gurpsbassi: But By.className("whsOnd zHQkBf") will only accept one class name by definition.

@Chima: Why not use

driver.findElement(By.xpath("//input[@name='password']"));

unless you have two elements with same name you should be able to resolve it.
Top Expert 2015

Commented:
question is how can you uniquely identify the element in question.
If you really need both class names to be present then use By.cssSelector(....) where you can define a selector to match on both classnames.

Also you might want to consider shortening your search path by starting from the current element rather than top of the DOM tree.

Author

Commented:
Siva Prasanna Kuma,  I'll read the link you have provided.
In answer to your question; use driver.findElement(By.xpath("//input[@name='password']"));   I am working with MS Edge and it is not like firefox where one has firepath.  I can't "roll my own" xpath at this time.  Possibly I'll learn how later.  I was  learning that I can get xpath from firefox/firepath and use it in Edge....Is this true?  I tried it and it did not work, but I'll have to look at it again.

gurpsbassi  Your comment: use By.cssSelector(....) where you can define a selector to match on both classnames
I'll try it.  I might have a follow up question.

A comment for both of you; The site I was given to work with is/was gmail.com  I had my code working, but then gmail changed their website.  The other site is/was facebook.  I do not know why it is not picking up the "login" button, and my code worked at one time.
If I have time, I'll go back to this code, but I have to move on.  If this happens, then I will issue points and close this question.
Thanks
Top Expert 2015
Commented:
China do you understand what I've said? Do you appreciate there are multiple class names?

Author

Commented:
gurpsbassi, Yes and I'll look into that.  I just do not have time at the moment.
Top Expert 2015

Commented:
Right well. Whenever you have time then.

Author

Commented:
gurpsbassi and siva, I have time today to look into this, yet I decided to close this question and open a new one if I need to.
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial