Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
555 views
in Technique[技术] by (71.8m points)

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]

how to click on button Download bounding boxes on python by selenium , the 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The button is in <a> tag, not <div> tag. You also have only one <div> so div[1] is invalid. 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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...