【问题标题】:Python: Selenium cannot click XPath text element [duplicate]Python:Selenium 无法单击 XPath 文本元素 [重复]
【发布时间】:2017-11-22 09:52:21
【问题描述】:

我正在尝试单击此 HTML 的元素:

<div class="feed-item__explanation" style="padding-left: 15px;">
     <a href="javascript:" ng-click="carinext()">Load more</a>
</div>

我正在使用 selenium 来处理这个:

driver.get(url)
while True:
        try:
            driver.find_element_by_xpath('//a[text()="Load more"]')
            print 'found'
        except Exception as e:
            print e
            break

可以处理该代码并给出“找到”输出。 但是当我试图点击那个元素时,

driver.find_element_by_xpath('//a[text()="Load more"]').click()

我收到这样的错误:

消息:元素不可见 (会话信息:chrome=62.0.3202.94)

这是处理 AngularJS 脚本的问题吗?

【问题讨论】:

    标签: python angularjs selenium xpath


    【解决方案1】:

    您可能需要向下滚动到所需的元素才能单击它。试试下面的代码:

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC 
    
    load_more = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.LINK_TEXT, "Load more")))
    driver.execute_script('arguments[0].scrollIntoView();', load_more)
    load_more.click()
    

    【讨论】:

    • 我试过了,但没有任何消息,比如Message:
    • 如果用WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Load more"))).click()替换load_more.click()会怎样?
    • 哦,对不起,我有错字,第一个解决方案就是答案,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    • 2019-03-19
    • 1970-01-01
    • 2018-06-11
    相关资源
    最近更新 更多