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
SilentRocket7478
SilentRocket7478OP4w ago
IWebElement firstButton = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[@class='FBYjn gK0xL A7Cr_ m3ODJ']")));
IWebElement firstButton = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[@class='FBYjn gK0xL A7Cr_ m3ODJ']")));
i am using Chrome the thing is, it clicks buttons with only 1 word in its class
Angius
Angius4w ago
IIRC XPath doesn't have anything like "a list of classes"
SilentRocket7478
SilentRocket7478OP4w ago
what does that mean ? that is how the html looks
Angius
Angius4w ago
Sure
SilentRocket7478
SilentRocket7478OP4w ago
<button type="button" class="FBYjn gK0xL A7Cr_ m3ODJ"></button>
<button type="button" class="FBYjn gK0xL A7Cr_ m3ODJ"></button>
Angius
Angius4w ago
But I don't think XPath supports multiple classes in a @class
SilentRocket7478
SilentRocket7478OP4w ago
what can i do then?
Angius
Angius4w ago
Maybe //a[contains(@class, 'FBYjn') and contains(@class, 'gK0xL') and contains(@class, 'A7Cr_') and contains(@class, 'm3ODJ')]?
SilentRocket7478
SilentRocket7478OP4w ago
div? where u get //div from?
Angius
Angius4w ago
Well, a in your case There, fixed
SilentRocket7478
SilentRocket7478OP4w ago
it is a button, not "a"
Angius
Angius4w ago
No description
SilentRocket7478
SilentRocket7478OP4w ago
should i replace it?
Angius
Angius4w ago
//a[contains(concat(' ',normalize-space(@class),' '),' FBYjn gK0xL A7Cr_ m3ODJ ')] could also work
SilentRocket7478
SilentRocket7478OP4w ago
i think i used wrong code wait should there be even //a ?
Angius
Angius4w ago
If you want to match an anchor tag, then yes
SilentRocket7478
SilentRocket7478OP4w ago
it is a button we are talking about, as you know, maybe.
Angius
Angius4w ago
//button then, not //a
SilentRocket7478
SilentRocket7478OP4w ago
alright then how is it looking?
//button[contains(@class, 'FBYjn') and contains(@class, 'gK0xL') and contains(@class, 'A7Cr_') and contains(@class, 'm3ODJ')]
//button[contains(@class, 'FBYjn') and contains(@class, 'gK0xL') and contains(@class, 'A7Cr_') and contains(@class, 'm3ODJ')]
Angius
Angius4w ago
Try it
SilentRocket7478
SilentRocket7478OP4w ago
ok
Angius
Angius4w ago
There are XPath checkers out there
SilentRocket7478
SilentRocket7478OP4w ago
what does that mean? now i got this
IWebElement firstButton = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[contains(@class, 'FBYjn') and contains(@class, 'gK0xL') and contains(@class, 'A7Cr_') and contains(@class, 'm3ODJ')]")));
IWebElement firstButton = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[contains(@class, 'FBYjn') and contains(@class, 'gK0xL') and contains(@class, 'A7Cr_') and contains(@class, 'm3ODJ')]")));
looking good?
Angius
Angius4w ago
I'd say so
Angius
Angius4w ago
The online XPath tester says it's good:
Free Online XPath Tester / Evaluator - FreeFormatter.com
This free online XPath tester lets you test your expressions/queries against an XML file
No description
SilentRocket7478
SilentRocket7478OP4w ago
it still does not click, like it does not finds it
Angius
Angius4w ago
Debug, then
SilentRocket7478
SilentRocket7478OP4w ago
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
Angius
Angius4w ago
There's no dotnet debug command
SilentRocket7478
SilentRocket7478OP4w ago
in visual studio code
Angius
Angius4w ago
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
SilentRocket7478
SilentRocket7478OP4w ago
the small red dot?
Angius
Angius4w ago
Yes
SilentRocket7478
SilentRocket7478OP4w ago
ok and then i run it?
Angius
Angius4w ago
With the debug profile, yes $debug
MODiX
MODiX4w ago
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.
Angius
Angius4w ago
Ah, that's a VS tutorial, not VSCode one, oops
SilentRocket7478
SilentRocket7478OP4w ago
ok nothing happens i added a breakpoint i ran i did debug ofcourse but won-t click lemme show ya this
Angius
Angius4w ago
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
SilentRocket7478
SilentRocket7478OP4w ago
Console.WriteLine("Looking for the first button...");

IWebElement firstButton = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[contains(@class, 'FBYjn') and contains(@class, 'gK0xL') and contains(@class, 'A7Cr_') and contains(@class, 'm3ODJ')]")));

Console.WriteLine($"First button found. Text: {firstButton.Text}, Location: {firstButton.Location}, Size: {firstButton.Size}");
Console.WriteLine("Looking for the first button...");

IWebElement firstButton = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[contains(@class, 'FBYjn') and contains(@class, 'gK0xL') and contains(@class, 'A7Cr_') and contains(@class, 'm3ODJ')]")));

Console.WriteLine($"First button found. Text: {firstButton.Text}, Location: {firstButton.Location}, Size: {firstButton.Size}");
Angius
Angius4w ago
Does Looking for the first button... show in the console?
SilentRocket7478
SilentRocket7478OP4w ago
yes
Angius
Angius4w ago
I guess your debugging went wrong, then Alas, I don't have much experience writing and debugging C# in VSCode
SilentRocket7478
SilentRocket7478OP4w ago
i will try the code in visual studio
Angius
Angius4w ago
Ideally, that's what you should be using, yeah
SilentRocket7478
SilentRocket7478OP4w ago
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
Angius
Angius4w ago
Try the other XPath I posted
SilentRocket7478
SilentRocket7478OP4w ago
ok
Angius
Angius4w ago
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, classes
SilentRocket7478
SilentRocket7478OP4w ago
haha 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
Angius
Angius4w ago
What of By.CssSelector?
SilentRocket7478
SilentRocket7478OP4w ago
? what? both did not work
Angius
Angius4w ago
No description
Angius
Angius4w ago
Does this one work?
SilentRocket7478
SilentRocket7478OP4w ago
no
Angius
Angius4w ago
Could maybe try ByAll And chain multiple ByClassName like that
SilentRocket7478
SilentRocket7478OP4w ago
nope it produces an error it dose not exist in this context
Angius
Angius4w ago
new ByAll(By.ClassName(...), By.ClassName(...)) Highly recommend reading the docs
SilentRocket7478
SilentRocket7478OP4w ago
i 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
Angius
Angius4w ago
:Ok:
ckam03
ckam034w ago
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
SilentRocket7478
SilentRocket7478OP4w ago
@ckam03What if it was a button inside tons of divs? is it possible to click the button directly?
ckam03
ckam034w ago
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?
SilentRocket7478
SilentRocket7478OP3w ago
I got it fixed

Did you find this page helpful?