【问题标题】:Element Exception元素异常
【发布时间】:2018-03-15 00:24:50
【问题描述】:

我正在尝试执行此代码,它显示“NoSuchElementException”这个错误。 那么任何人都可以帮助我吗?

代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver= webdriver.Chrome()

page=driver.get("http://www.awgp.org")
banner_tell=driver.find_element_by_Link_text('Tell Me More')
banner_tell.click()

【问题讨论】:

    标签: python python-2.7 selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    我认为您需要做的就是以大写形式提供链接文本,但此链接是动态的,因为横幅会自动滑动到下一个。

    您应该想出另一个定位器来准确点击您想要点击的定位器。否则,如果更改横幅,您可能会收到ElementNotVisibleException

    banner_tell=driver.find_element_by_link_text('TELL ME MORE')
    

    【讨论】:

    • find_element_by_link_text() 仅返回可见元素,因此无论如何您都不会得到ElementNotVisibleException ...另外,是的,应该使用'TELL ME MORE',因为这正是链接文本在页面上的显示方式(+1 )
    • @Andersson- 谢谢!链接是动态的,当显示下一个横幅时,它们的显示属性设置为none。因此,如果横幅在被点击之前发生了变化,它会给出异常。
    • 是的,但是如果链接不可见,find_element_by_link_text() 将无法找到它,所以应该提高NoSuchElementException 而不是ElementNotVisibleException...
    • 谢谢@Amit。 “告诉我更多”工作正常,因为它可以在整个页面中找到我试图在横幅中找到链接的文本。
    【解决方案2】:

    试试 xpath

    banner_tell= driver.find_element_by_xpath("//*[contains(text(),'TELL ME MORE')]")
    

    【讨论】:

    • 我无法使用 xpath 定位元素。 banner_tell= driver.find_element_by_xpath("//input[contains(@text,'TELL ME MORE')]")
    【解决方案3】:

    似乎你几乎在附近,但 function() 应该是:

    find_element_by_link_text()
    

    要单击文本为 TELL ME MORE 的按钮,您必须使用 expected_conditions 子句 element_to_be_clickable 诱导 WebDriverWait,如下所示:

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC    
    
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "TELL ME MORE"))).click()
    

    【讨论】:

      猜你喜欢
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 2012-03-20
      • 2015-03-11
      • 2020-08-07
      • 2017-05-28
      • 2018-04-10
      • 1970-01-01
      相关资源
      最近更新 更多