【问题标题】:python selenium click on button without Id and namepython selenium单击没有ID和名称的按钮
【发布时间】:2019-08-25 00:05:00
【问题描述】:

我有一个按钮,我想点击这个按钮,我愿意

login_form = driver.find_element_by_xpath("/html/body/div/h1/div[1]").click();

我的代码:

driver = webdriver.Firefox()
driver.get('http://www.textdet.com/')

e = driver.find_element_by_id("imagefile")                  
e.send_keys("/home/brm17/Desktop/ProjetFinDetude/image.png")  
login_form = driver.find_element_by_xpath("/html/body/div/h1/div[1]").click();

但我明白了:

selenium.common.exceptions.NoSuchElementException:消息:无法 定位元素:/html/body/div/h1/div[1]

如何在python by selenium 上点击按钮Download bounding boxes,html

<h1 style="position: relative"><a href="/">Scene Text Detection Demos</a>

        <div aria-label="..." role="group" class="btn-group" style="position: absolute; bottom: 10px; right: 0px;">
            <!--<button id="toggle_text_propoals" type="button" class="btn btn-success btn-sm">Show text proposals</button>-->
            <a href="download_bboxes/acdb4bb9-8b73-4020-bfe5-737316033e5e" type="button" class="btn btn-success btn-sm">Download bounding boxes</a>
        </div>

      </h1>

【问题讨论】:

    标签: python selenium selenium-webdriver


    【解决方案1】:

    按钮位于&lt;a&gt; 标记中,而不是&lt;div&gt; 标记中。您也只有一个&lt;div&gt;,所以div[1] 无效。你可以按文字搜索

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    button = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//a[contains(., "Download bounding boxes")]')))
    button.click();
    

    或者按班级,因为你只有一个按钮,所以班级btn 应该是唯一的。

    driver.find_element_by_class_name('btn').click(); # or 'btn-success' or 'btn-sm'
    

    顺便说一句,click() 不返回任何东西,你不能将它分配给login_form

    【讨论】:

    • 错误selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //a[contains(., "Download bounding boxes")]
    • 问题 :` button = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//a[contains(., "下载边界框")]')) ) NameError: name 'WebDriverWait' is not defined`
    • @Brahimce 你添加了导入吗?
    • 另一个错误 ` button = WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.XPATH, '//a[contains(., "Download bounding boxes")]') )) NameError: name 'By' is not defined `
    • 我想在页面加载后点击“下载边界框”按钮
    【解决方案2】:

    按钮位于&lt;a&gt; 标签下,所以我们为 Xpath 编写代码:

     driver.find_element_by_xpath("//a[. = 'Download bounding boxes']").click()
    

    所以它在网站上发现文本是“下载边界框”并点击它。 在上面的代码中,// a 是因为按钮在&lt;a&gt; 标签内而写的,如果代码在spandiv标签下,我们也可以写//span,//div

    【讨论】:

      猜你喜欢
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-05
      • 2021-02-18
      相关资源
      最近更新 更多