4

I want to click on Administration panel. The <span> contains a class attribute and inner text.

I get an error saying "unable to locate element". What am I doing wrong?

HTML:

 <div class="item-inner">
 <span class="title"> Administration </span>
 <i class="icon-arrow"></i>
 </div>

C# code

 var admi = driver.FindElement(By.XPath("//span[contains(@class,'title')] [contains(text(),'Administration')]"));
 admi.Click();
Kate Paulk
31.5k8 gold badges56 silver badges109 bronze badges
asked Nov 18, 2015 at 11:53
2
  • var admi = driver.FindElement(By.XPath("//div[contains(@class,'title')] [contains(text(),'Administration')]")); Try this Commented Nov 18, 2015 at 12:22
  • have you soved this problem?i meet the same problem! Commented Feb 20, 2023 at 10:10

5 Answers 5

3

Try to avoid the use of contains as your method of finding an element. This will mainly cause you issues later down the line when other elements unintentionally match

I would suggest you use something more like;

driver.FindElement(By.XPath("//div[@class='item-inner']/span[@class='title']"));

This example still isn't ideal, and ideally you should ask your developers to add in a unique ID for you to utilize.

alecxe
11.4k11 gold badges52 silver badges107 bronze badges
answered Nov 18, 2015 at 12:58
0
0

Use following Xpath

driver.findElement(By.xpath("//span[@class='title']"));

Also make sure variable in which you are saving the element is of WebElement type.

answered Nov 20, 2015 at 12:26
0

var admi = driver.FindElement(By.XPath("//span[contains(@class,'title') and contains(text(),'Administration')]"));

You were almost there.

answered Jul 4, 2019 at 6:42
0

If you are using Pandas you can search by:

url = "YOUR_URL_HERE"
# create a new Firefox session
driver = webdriver.Firefox()
driver.implicitly_wait(1)
driver.get(url)
result = driver.find_elements_by_class_name('screen-reader-text')
pprint(result)
Kate Paulk
31.5k8 gold badges56 silver badges109 bronze badges
answered Jul 3, 2019 at 14:12
0

If the span element is the only element on the page with the ClassName "title", you could just get the element by ClassName:

IWebElement admin = driver.FindElement(By.ClassName("title"));

If the span element is not the only element with the ClassName "title", but is the only element with that ClassName under its parent, you can get the parent element then the span element:

IWebElment admin = driver.FindElement(By.ClassName("item-inner")).FindElement(By.ClassName("title"));

Otherwise, I would follow the advise of others using the XPath. Usually I try to use other methods of finding elements because XPath can be slow or unreliable.

answered Jul 5, 2019 at 15:31

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.