【问题标题】:Python Selenium: Having trouble fixing NoSuchElementException for a specific buttonPython Selenium:修复特定按钮的 NoSuchElementException 时遇到问题
【发布时间】:2019-08-29 10:36:59
【问题描述】:

我正在尝试在 python 中按下带有 selenium 的按钮。我无法使用 xpath 或 css 选择器找到它。

对于脚本中的其他内容,我一直在使用 xpath,它工作正常,但每次打开此按钮的 xpath 似乎都是相对的。因此,我尝试使用 css 选择器访问。

我尝试访问的元素如下所示:

<button type="button" data-dismiss="modal" class="btn btn-primary pull-right">Opret AnnonceAgent</button>

chrome中inspect的css选择器:

#\35 d167939-adc2-0901-34ea-406e6c26e1ab > div.modal-footer > div > button.btn.btn-primary.pull-right

如果我应该发布更多 html,请告诉我。

我尝试了许多堆栈 oveflow 问题,并尝试了 css 选择器的许多变体,例如:

  • driver.find_element_by_class_name('#\35 d167939-adc2-0901-34ea-406e6c26e1ab.div.modal-footer.div.button.btn.btn-primary.pull-rightdiv.button.btn.btn-primary-pull-right').click()

  • driver.find_element_by_class_name('div.button.btn.btn-primary-pull-right').click()

  • driver.find_element_by_class_name('button.btn.btn-primary-pull-right').click()

  • driver.find_element_by_class_name('btn.btn-primary-pull-right').click()

  • driver.find_element_by_class_name('btn-primary-pull-right').click()

我也试过睡眠定时器。

该按钮位于当您按下前一个按钮时打开的窗口中,如果有帮助,背景会显示为灰色。 Picture.

# This opens up the window in which to press the next button (works fine)
button = driver.find_element_by_xpath('//*[@id="content"]/div[1]/section/div[2]/button')
button.click()
driver.implicitly_wait(15)
time.sleep(5)
# This is what doesn't work
driver.find_element_by_class_name('button.btn.btn-primary-pull-right').click()

我希望程序按下按钮,但它没有,它就在那里。

【问题讨论】:

  • 试试看是否有效driver.find_element_by_css_selector('button.btn.btn-primary.pull-right').click()
  • 谢谢,但还是不行。我得到AttributeError: 'list' object has no attribute 'click',假设因为它是元素[s] - 这是故意的吗?
  • 打错字了。再试一次driver.find_element_by_css_selector('button.btn.btn-primary.pull-right').click()
  • 不行,我得到NoSuchElementException
  • 你能检查一下是否有框架阻止定位按钮吗?

标签: python selenium selenium-webdriver webdriver webdriverwait


【解决方案1】:

所需的元素位于 模态对话框中,因此您需要诱导 WebDriverWait 以使所需的元素可点击,您可以使用以下任一Locator Strategies:

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-primary.pull-right[data-dismiss='modal']"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-primary pull-right' and @data-dismiss='modal'][text()='Opret AnnonceAgent']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

【讨论】:

    【解决方案2】:

    class_name 接收一个类作为参数,但您使用css 语法作为定位器。

    css_selector

    driver.find_element_by_css_selector('button.btn.btn-primary-pull-right')
    

    这意味着&lt;button&gt;标签有2个类,btnbtn-primary-pull-right

    class_name

    driver.find_element_by_class_name('btn-primary-pull-right')
    

    【讨论】:

    • @BertilJohannesIpsen 请详细说明。究竟发生了什么?
    • 对,对不起!我遇到了同样的例外,NoSuchElementException: Message: no such element: Unable to locate element: {"method":"class name","selector":"btn-primary-pull-right"},另一个也同样如此。
    • @BertilJohannesIpsen 尝试添加explicit wait。您还应该检查按钮是否在&lt;iframe&gt;
    • 谢谢,我已经尝试过这两种方法(time.sleep(t) 以及线程@KajulKundu)
    【解决方案3】:

    我的猜测是正确的。有一个iframe 停止访问该元素。您必须先切换到iframe 才能访问button 元素。立即使用以下代码尝试。

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions
    
    #WebDriverWait(driver,20).until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH,'//iframe[starts-with(@id,"rdr_")]')))
    WebDriverWait(driver,20).until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH,'//iframe[@id="qualaroo_dnt_frame"]')))
    driver.find_element_by_css_selector('button.btn.btn-primary').click()
    

    【讨论】:

    • 你能造出这个词吗?当我实现它时,我得到一个TimeoutException
    • 做好一件事试试这个WebDriverWait(driver,20).until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH,'//iframe[@id="qualaroo_dnt_frame"]'))) driver.find_element_by_css_selector('button.btn.btn-primary').click()
    • 嗯,对于那个我再次得到一个NoSuchElementException 按钮。
    • 你能做一件事而不是图像你能分享 html 以便我可以复制整个 html 并在我的笔记本电脑上测试有什么问题吗?
    • 您要找的是同一个 html 吗?当我看到 html 时我在这里找不到任何 iframe 但是当您发布图片时它显示的是 iframe?
    猜你喜欢
    • 2014-03-21
    • 2019-02-07
    • 2021-10-24
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多