Home > Mobile >  selenium how to locate this type of element?
selenium how to locate this type of element?

Time:12-11

This element is a arrow button which is used to expand a block. That element is a arrow button of linked-in website of messaging box. There is a messaging box in bottom right corner of linked-in website page. So I want to click on that arrow.I tried many paths but it gives me nosuchelement exception. The main thing is id="ember84" this id always changes of that element whenever we refresh page so this below path doesn't work

xpath("//button[@id='ember84']")

This is the HTML of that complete element

<button id="ember84"  xpath="1">  <li-icon aria-hidden="true" type="chevron-down-icon"  size="small"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" data-supported-dps="16x16" fill="currentColor"  width="16" height="16" focusable="false">
  <path d="M1 5l7 4.61L15 5v2.39L8 12 1 7.39z"></path>
</svg></li-icon>

<span >
    You are on the messaging overlay. Press enter to minimize it.
</span></button>

I tried this path also but it gives nosuchelement exception

msg = driver.find_element_by_xpath('//button[1]//li-icon[1]//span[constains(text(), "You are on the messaging overlay. Press enter to minimize it.")]')

enter image description here so, how to locate that element ?

CodePudding user response:

Fist lets use these imports to find elements

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Next if you look at the html around the button, it is in a div with unique text in the class, msg-overlay-bubble-header__controls. Inside that div are two buttons, from the sceenshot it looks like you want the second one. So you can get the minimize button with the following:

minimize_button = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH,"//div[contains(@class, 'msg-overlay-bubble-header__controls')]//button")))[1]
  • Related