0

I have the following html:

<p class="k-reset"><a class="k-icon k-i-expand" href="#" tabindex="-1"></a>Unit: QA Room: 1</p>

I can't seem to get valid syntax to click on this element. I've tried the following:

IWebElement webElement5 = Driver.Instance.FindElement(By.XPath("//a[@class='k-icon k-i-expand']"));
webElement5.Click();
IWebElement webElement5 = Driver.Instance.FindElement(By.XPath("//p[text(), 'Unit: QA Room: 1']"));
webElement5.Click();

When I try to use the text(), I get an error stating that it is not a valid XPath expression. Everywhere I look on the internet uses that syntax. I'm very new to c#/Selenium/XPath values. Any help is very much appreciated.

1
  • The syntax is not correct. If you want text equals then you want //p[.='Unit: QA Room: 1']. If you want text contains then you want //p[contains(., 'Unit: QA Room: 1')]. You seem to be mixing the two... replace the , with a = and it should be valid. Commented Mar 14, 2019 at 15:32

2 Answers 2

2

You mixed partial syntax of contains

"//p[contains(text(), 'Unit: QA Room: 1')]"

For direct match use =

"//p[text()='Unit: QA Room: 1']"
Sign up to request clarification or add additional context in comments.

2 Comments

Do note that //p[text(), 'Unit: QA Room: 1'] is valid XPath 2.0. This will select every p element because the truth value of a sequence is false only when it's empty.
@Alejandro The browsers support by default xpath 1.0, you can test it in dev tools, therefor so is Selenium. That's why the OP got invalid syntax error.
0

To click on the element you can use either of the following solution:

  • CssSelector:

    Driver.Instance.FindElement(By.CssSelector("p.k-reset>a.k-icon.k-i-expand")).Click();
    
  • XPath 1:

    Driver.Instance.FindElement(By.XPath("//p[@class='k-reset']/a[@class='k-icon k-i-expand']")).Click();
    
  • XPath 2:

    Driver.Instance.FindElement(By.XPath("//p[@class='k-reset' and normalize-space()='Unit: QA Room: 1']")).Click();
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.