【问题标题】:Unable to locate and click an element using Selenium Webdriver [duplicate]无法使用 Selenium Webdriver 定位和单击元素 [重复]
【发布时间】:2020-03-11 13:35:01
【问题描述】:

我一直在尝试自动执行下载过程,但找不到需要点击的元素。该元素没有 ID,并且嵌入在表格中。我已经尝试过 xPath,但它没有用。

这是我要点击的元素的 html 代码-

image xlink:href="somerandomlink" class="tab-button-zone-image" width="30" height="9"></image

元素还有一个outerwidget代码,在它下面可以找到上面我要点击的实际元素的代码-

<div class="tab-zone tab-widget tabSuppressVizTooltipsAndOverlays tabZone-dashboard-object fade-bg" id="tabZoneId26" style="z-index: 35; width: 30px; height: 22px; top: 78px; left: 1315px;">

我一直在使用的代码是这样的:

elem1= driver.find_element_by_id('tabZoneId26')
elem1.click()

elem= driver.find_element_by_id('//*[@id="tabZoneId26"]/div/div/div/div/div/div/svg/image')
elem.click()

两者都不起作用。恐怕无法分享该页面的链接。

任何帮助将不胜感激,现在坚持了一段时间。谢谢。

【问题讨论】:

    标签: python html selenium automation


    【解决方案1】:

    您对 xpath 使用了错误的方法 find_element_by_id。右边是 find_element_by_xpath

    driver.find_element_by_xpath('//*[@id="tabZoneId26"]/div/div/div/div/div/div/svg/image')
    

    你的 xpath 也是不可信的:试着做更好的 xpath 像

    driver.find_element_by_xpath('//div[contains(@id,"tabZoneId2")]//svg/image[@class="tab-button-zone-image"]')
    

    在点击之前确保网页元素在 DOM 中。例如

    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as ec
    from selenium.webdriver.common.by import By
    
    timeout = 5
    wait = WebDriverWait(browser, timeout, poll_frequency=1)
    wait.until(
      method=ec.presence_of_element_located((By.XPATH, "YOUR XPATH")))
    

    【讨论】:

      【解决方案2】:
      elem1= driver.find_element_by_xpath('//div[@id="tabZoneId26"]//image')
      elem1.click()
      

      from selenium.webdriver.common.action_chains import ActionChains
      
      elem1 = driver.find_element_by_xpath('//div[@id="tabZoneId26"]//image')
      actions = ActionChains(driver)
      actions.click(elem1).perform()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-11
        • 2016-04-15
        • 1970-01-01
        • 2015-09-01
        • 2023-03-10
        • 1970-01-01
        • 2016-12-10
        相关资源
        最近更新 更多