Home > database >  Not able to search a div element using Selenium
Not able to search a div element using Selenium

Time:12-12

<div >
   <ul  role="menu">
      <li>
         <a>
            <div >
               <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16" ></svg>
            </div>
            <span  aria-label="Panel header item View">View</span>
            <span >
               <div >
                  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16" ></svg>
               </div>
               v
            </span>
         </a>
      </li>
      <li>
         <a>
            <div >
               <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16" ></svg>
            </div>
            <span  aria-label="Panel header item Share">Share</span>
            <span >
               <div >
                  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16" ></svg>
               </div>
               p s
            </span>
         </a>
      </li>
      <li >
         <a>
            <div >
               <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16" ></svg>
            </div>
            <span  aria-label="Panel header item Inspect">
               Inspect
               <div >
                  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16" ></svg>
               </div>
            </span>
            <span >
               <div >
                  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16" ></svg>
               </div>
               i
            </span>
         </a>
         <ul  role="">
            <li><a><span  aria-label="Panel header item Data">Data</span></a></li>
            <li><a><span  aria-label="Panel header item Panel JSON">Panel JSON</span></a></li>
         </ul>
      </li>
   </ul>
</div>

I want to click on Data as shown in the pic enter image description here. The HTML code above contains the code for this list.

I am not able to search/find main menu by using :

driver.find_element(By.CLASS_NAME,"panel-menu-container dropdown open")

and getting error enter image description here Kindly help me out.

def scrape_data():
    DRIVER_PATH = r"C:\chrome\chromedriver.exe"
    driver = webdriver.Chrome(DRIVER_PATH)
    driver.get('Link to the dashboard')
    time.sleep(20)    
    driver.find_element(By.XPATH,"//div[@class='panel-menu-container dropdown open']")

CodePudding user response:

In case you want to click the parent div element with class name values panel-menu-container dropdown open this can be done with following XPath or CSS Selector.
It should not be done by CLASS_NAME since CLASS_NAME accepts single class name value while this element contains 3 class name values: panel-menu-container dropdown open
So, with CSS Selector:

driver.find_element(By.CSS_SELECTOR,".panel-menu-container.dropdown.open")

Or

driver.find_element(By.CSS_SELECTOR,"div.panel-menu-container.dropdown.open")

With XPath:

driver.find_element(By.XPATH,"//*[@class='panel-menu-container dropdown open']")

Or

driver.find_element(By.XPATH,"//div[@class='panel-menu-container dropdown open']")

In case you want to click the Data option this XPath should work:

driver.find_element(By.XPATH,"//span[contains(text(),'Data')]")

In case you prefer CSS Selectors - this can also be done by CSS Selector:

driver.find_element(By.CSS_SELECTOR,"[aria-label='Panel header item Data']")

Or

driver.find_element(By.CSS_SELECTOR,"span[aria-label='Panel header item Data']")
  • Related