pythonselenium-webdriverweb-scrapingclick

Automate clicking a <div role='button'> using Selenium


Trying to automate connecting to people from a list and there's three places the button is present. two scenarios work, but the third doesn't.

Problem: on line [4], for connect_button, .click() directly doesn't work

Error Code: Element not interactable

Website: Linkedin Person's Profile Page

HTML:

<li>      
    <div aria-label="Invite XXXX XXXX to connect" role="button" id="ember1454" class="artdeco-dropdown__item artdeco-dropdown__item--is-dropdown ember-view full-width display-flex align-items-center" tabindex="0">
<!---->
        <svg role="none" aria-hidden="true" class="mr3 flex-grow-0" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" data-supported-dps="24x24" data-test-icon="add-medium">
<!---->    
    <use href="#add-medium" width="24" height="24"></use>
</svg>

      <span class="display-flex t-normal flex-1" aria-hidden="true">Connect</span>
    </div>
        </li>

My code:

more_button = driver.find_element(By.XPATH, "//div[contains(@class,'mdOdWeQQtwVxGMjVkbvrASsRjvlYIwwNC')]//button[contains(@aria-label,'More actions')]/span")
more_button.click()
connect_button = driver.find_element(By.XPATH, "//div[@class='artdeco-dropdown__content-inner']//ul//li[3]//div[@role='button']/span")
connect_button.click() # PROBLEM LINE, would like alternates for this
time.sleep(random.uniform(3, 5))
connectto_button = driver.find_element(By.XPATH, connectoverlaybuttonxpath)
connectto_button.click()
time.sleep(random.uniform(1, 3))

Solution

  • If you try to console.log("connectto_button"), does it shows an element or undefined ? If not, maybe the element is hidden, so the click might not work.

    Also, the second XPath returns multiple elements and the driver.find_element returns only the first one, which is a bad one.