使用 Selenium 自动点击 <div role='button'>

问题描述 投票:0回答:1
尝试自动连接到列表中的人员,并且该按钮出现在三个位置。两种情况有效,但第三种情况无效。

问题:.click()直接不起作用

错误代码:元素不可交互

网站: Linkedin 人员的个人资料页面

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>
我的代码:

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))
    
python selenium-webdriver web-scraping click
1个回答
0
投票
如果您尝试 console.log("connectto_button"),它是否显示一个元素或未定义? 如果没有,可能该元素被隐藏了,因此单击可能不起作用。

保罗

© www.soinside.com 2019 - 2024. All rights reserved.