1

i using selenium try to get the div class value in html, but i hit the problem as per below

invalid selector: Unable to locate an element with the xpath expression //div[contains(., 'Medium') because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[contains(., 'Medium')' is not a valid XPath expression.

Below is my html and my code that read value of html.

HTML:

enter image description here

Code trials:

IWebElement element = driver.FindElement(By.XPath("//div[contains(., 'Medium')"));
String text =   element.GetAttribute("text");
Console.WriteLine(text);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
JonathanK
  • 37
  • 5

2 Answers2

1

This error message...

invalid selector: Unable to locate an element with the xpath expression //div[contains(., 'Medium') because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[contains(., 'Medium')' is not a valid XPath expression.

...implies that the XPath which you have used was not a valid XPath expression.


You were so close. You missed the closing ] while constructing the .

Effectively your line of code will be:

IWebElement element = driver.FindElement(By.XPath("//div[contains(., 'Medium')]"));

References

You can find a couple of related relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

You xpath expression less closing ]:

IWebElement element = driver.FindElement(By.XPath("//div[contains(., 'Medium')]"));
String text = element.Text;
Console.WriteLine(text);

And to extract text use element.Text; or element.GetAttribute("innerHTML");

frianH
  • 7,295
  • 6
  • 20
  • 45
  • It working, thanks!!! but i still not able get the text value in in console. – JonathanK Jun 09 '20 at 04:15
  • @JonathanK try `element.GetAttribute("innerHTML");` or `element.Text;` – frianH Jun 09 '20 at 04:16
  • 1
    Actually I wanted to get the value of " Medium is not like other platform on the internet" thank you for the help – JonathanK Jun 09 '20 at 04:20
  • thank you..do you know how to set the value to the element ? i wanted to change the value " Medium is not like other platform on the internet" to other wording. – JonathanK Jun 09 '20 at 04:34
  • @JonathanK no, it's inside `
    ` tag, if by manually you can't change the text, likewise with selenium.
    – frianH Jun 09 '20 at 04:40