0

i have a problem with locating element with xPath.

<li>
  <a aria-current="false" href="/drop"> 
    <button tabindex="0" type="button" style="border: 10px; box-sizing: border-box; display: inline-block; font-family: Roboto, sans-serif; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); cursor: pointer; text-decoration: none; margin: 0px; padding: 0px; outline: none; font-size: inherit; font-weight: inherit; position: relative; height: 36px; line-height: 36px; min-width: 88px; color: rgba(0, 0, 0, 0.87); transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms; border-radius: 2px; user-select: none; overflow: hidden; background-color: rgba(0, 0, 0, 0); text-align: center; top: -7px;">
    <div>
      <span class="material-icons" color="rgba(0, 0, 0, 0.87)" style="color: rgba(0, 0, 0, 0.87); position: relative; font-size: 18px; display: inline-block; user-select: none; transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms; vertical-align: middle; margin-left: 12px; margin-right: 0px;">
        pin_drop
      </span>
      <span style="position: relative; padding-left: 8px; padding-right: 16px; vertical-align: middle; letter-spacing: 0px; text-transform: uppercase; font-weight: 500; font-size: 14px;">Drop</span>
    </div>
    </button>
  </a>
</li>
browser.find_element_by_xpath('//*[@id="root"]/div/div[1]/div[1]/nav/ul/li[7]/a/button/div/span[3]').click()

Hey, i tried using xPath and other method seemms it can't find this element.

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

2 Answers2

0

If you are trying to click on the Drop button then use the below xpath.

//li/a[@href="/drop"]//span[.='Drop']

enter image description here

Include below imports:

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

Your code should be:

WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH, "//li/a[@href='/drop']//span[.='Drop']"))).click()
supputuri
  • 13,644
  • 2
  • 21
  • 39
0

To click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li > a[href=/drop] > button span:nth-of-type(2)")))
    
  • Using XPATH:

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//li/a[@href='/drop']/button//span[text()='Drop']")))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • @LukasAlišauskas Check out the updated answer and let me know the status. – undetected Selenium Jan 10 '20 at 14:44
  • `raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:` Still i got error @DebanjanB When using xPath it shows no error but not working – Lukas Ališauskas Jan 10 '20 at 14:53
  • @LukasAlišauskas _TimeoutException_ is the outcome of **failed** _expected-conditions_. Debug your code through `find_element_by_*` in-conjunction with `time.sleep()`. If you are able to locate the element, update the question with the observations. Check the HTML and traverse upwards and observe if the element is within an – undetected Selenium Jan 10 '20 at 22:25
  • Tried a lot of thing but still not wroking read full discuss still i got error there is no – Lukas Ališauskas Jan 11 '20 at 10:59
  • I found way to do it `driver.get('https://link/drop') ` like so but there is button and i can't click it same problem – Lukas Ališauskas Jan 11 '20 at 11:21