A line of C# code is not working with no error
I am trying to click a button in a webpage using selenium and C# but it is not working with buttons that has more than one class
65 Replies
i am using Chrome
the thing is, it clicks buttons with only 1 word in its class
IIRC XPath doesn't have anything like "a list of classes"
what does that mean ?
that is how the html looks
Sure
But I don't think XPath supports multiple classes in a
@class
what can i do then?
Maybe
//a[contains(@class, 'FBYjn') and contains(@class, 'gK0xL') and contains(@class, 'A7Cr_') and contains(@class, 'm3ODJ')]
?div?
where u get //div from?
Well,
a
in your case
There, fixedit is a button, not "a"
should i replace it?
//a[contains(concat(' ',normalize-space(@class),' '),' FBYjn gK0xL A7Cr_ m3ODJ ')]
could also worki think i used wrong code
wait
should there be even //a ?
If you want to match an anchor tag, then yes
it is a button we are talking about, as you know, maybe.
//button
then, not //a
alright then
how is it looking?
Try it
ok
There are XPath checkers out there
what does that mean?
now i got this
looking good?
I'd say so
Free Online XPath Tester / Evaluator - FreeFormatter.com
This free online XPath tester lets you test your expressions/queries against an XML file
it still does not click, like it does not finds it
Debug, then
Looking for the first button...
Created TensorFlow Lite XNNPACK delegate for CPU.
Error: Timed out after 30 seconds
ChromeDriver closed.
i did
dotnet debug
dotnet run
everytime
even when i do most minor things
i am not sure if i must to
but i am a bit new to C hashtag
There's no
dotnet debug
commandin visual studio code
Place a breakpoint on the line that's supposed to get the element, and see if you actually get it
Then step through the code and see where it goes
the small red dot?
Yes
ok
and then i run it?
With the debug profile, yes
$debug
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
Ah, that's a VS tutorial, not VSCode one, oops
Debugging C# in Visual Studio Code
See how you can run and debug your C# source code
ok
nothing happens
i added a breakpoint
i ran
i did debug ofcourse
but won-t click
lemme show ya this
If the code did not stop at the breakpoint, that means the code did not run
So maybe you're not calling the method or something
Does
Looking for the first button...
show in the console?yes
I guess your debugging went wrong, then
Alas, I don't have much experience writing and debugging C# in VSCode
i will try the code in visual studio
Ideally, that's what you should be using, yeah
so i moved everything there but the problem is still there
i tried to click another button that has only 1 class
it worked fine
i do not know what is its trouble with more than 1 class buttons
Try the other XPath I posted
ok
Or... instead of
By.XPath
try By.CssSelector("button.FBYjn.gK0xL.A7Cr_.m3ODJ")
Or By.ClassName("FBYjn gK0xL A7Cr_ m3ODJ")
, but this lets you select only by, well, classeshaha
the first one not productive
Looking for the first button...
Unexpected error: Compound class names not allowed. Cannot have whitespace in class name. Use CSS selectors instead.; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#invalid-selector-exception
ChromeDriver closed.
is it worth to mention that the button in inside a div ?@Angius
What of
By.CssSelector
??
what?
both did not work
Does this one work?
no
Could maybe try
ByAll
And chain multiple ByClassName
like thatnope
it produces an error
it dose not exist in this context
new ByAll(By.ClassName(...), By.ClassName(...))
Highly recommend reading the docsi think i will look into it tomorrow
im so tired
my eyes are closing between each message
i appreciate your time
i will definitly update you
:Ok:
Man I feel for you
It's simple
for xpath
//div[@class='FPdoLc lJ9FBc']
That's how you add multiple classes
for cssselector
By.CssSelector("textarea[class='gLFyf']"))
also just need a space
if you have a button inside of a div then you would do something like
//center//input[@class='gNO89b']
I recommend using css selectors though
with a cssSelector, the previous selector would be
center > input[class='gNO89b']
all you need is a space between classes
XPath needs an @ before class while cssSelector doesn't
@ckam03What if it was a button inside tons of divs?
is it possible to click the button directly?
yes
just keep adding div > div > div if you have to
or //div//div//div
I've had selectors like 10 elements deep before
Do you really need that though? There's usually a better way than having to go 10 elements deep. Is there no id or anything?
I got it fixed