如何使用Selenium和Python单击标记以展开“树视图”

问题描述 投票:2回答:1

这是我想要执行单击操作的标记。元素成功定位,click()事件也被接受,但没有执行任何操作。

<img class="pointer" id="id53" src="/images/tree/elbow-minus.gif" alt="collapse not last" onclick="$(this)[0].src='/images/ajax-indicator.gif';var wcall=wicketAjaxGet('wicket/page?1-1.IBehaviorListener.0-dashboardBody-columns-0-gadgets-1-body-content-treeTable-form-rows-4-expandLink',function() { }.bind(this),function() { }.bind(this), function() {return Wicket.$('id53') != null;}.bind(this));;return !wcall;">

码:

link_xpath = "//a[contains(text(),'git')]//parent::*/img"
driver.find_element_by_xpath(link_xpath).click()
python selenium xpath css-selectors webdriverwait
1个回答
0
投票

根据你提供的HTML,它是一个<img>标签,元素是一个动态元素。因此,要单击所需的元素,您必须引导WebDriverWait才能使元素可单击,您可以使用以下任一解决方案:

  • 使用CSS_SELECTORelement = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "img.pointer[src='/images/tree/elbow-minus.gif']"))).click()
  • 使用XPATHelement = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//img[@class='pointer' and @src='/images/tree/elbow-minus.gif']"))).click()
  • 注意:您必须添加以下导入: from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
© www.soinside.com 2019 - 2024. All rights reserved.