简体   繁体   English

python selenium单击没有ID和名称的按钮

[英]python selenium click on button without Id and name

I have one button, and I want to click on this button , I do 我有一个按钮,我想单击该按钮,

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

my code : 我的代码:

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();

But I get: 但是我得到:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: /html/body/div/h1/div[1] selenium.common.exceptions.NoSuchElementException:消息:无法找到元素:/ html / body / div / h1 / div [1]

how to click on button Download bounding boxes on python by selenium , the html 如何单击按钮selenium ,html在pythonDownload bounding boxes

<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>

The button is in <a> tag, not <div> tag. 该按钮位于<a>标记中,而不位于<div>标记中。 You also have only one <div> so div[1] is invalid. 您也只有一个<div>因此div[1]无效。 You can search by the text 您可以按文字搜索

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();

Or by class since you have only one button so class btn should be unique. 或者按类,因为您只有一个按钮,所以btn类应该是唯一的。

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

By the way, click() doesn't return anything, you can't assign it to login_form . 顺便说一下, click()不返回任何内容,您不能将其分配给login_form

The button is under an <a> tag so we write code for Xpath: 该按钮在<a>标记下,因此我们为Xpath编写代码:

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

so it finds the text is "Download bounding boxes" on the website and clicks on it. 因此它会在网站上找到“下载边界框”文本,然后单击它。 In the code above // a is written because the button was inside the <a> tag we can write //span , //div as well if the code was under a span or div tag. 在上面的代码中// a被编写是因为按钮位于<a>标记//span ,如果代码位于spandiv标记下,我们也可以编写//span//div

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM