如何在Python中使用Selenium点击按钮?

huangapple go评论69阅读模式
英文:

How can I click on a button using Selenium in Python?

问题

你可以尝试使用以下代码来点击按钮:

element = driver.find_element(By.XPATH, '//button[@title="Abwesenheit melden" and @class="btn btn-primary"]')
element.click()

这将查找具有标题"Abwesenheit melden"和类"btn btn-primary"的按钮,并执行点击操作。如果这不起作用,请确保你的Selenium版本与网站的HTML结构兼容,或者尝试使用不同的定位方式。

英文:

How can I click on a Button with my Python code? I write a programm, which should log me in on an website. Now i have to click on a button and it dosen't work. I tried every method I found on the Internet. It still wont work and I donk know why.

Currently I use Selenium 4.9.0

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

this is the Button from the HTML-Site:

<form class="un-form form-inline"><div><button type="button" title="Abwesenheit melden" class="btn btn-primary">Abwesenheit melden</button><button type="button" title="Löschen" class="btn btn-danger">Löschen</button><div class="dropup btn-group"><button id="8a4bb522-063d-4eca-a121-3f32933a3a1e" role="button" aria-haspopup="true" aria-expanded="false" type="button" class="dropdown-toggle btn btn-default">Entschuldigungsschreiben drucken <span class="caret"></span></button><ul role="menu" class="dropdown-menu" aria-labelledby="8a4bb522-063d-4eca-a121-3f32933a3a1e"><li role="presentation" class=""><a role="menuitem" tabindex="-1" href="#">pro Stunde</a></li><li role="presentation" class=""><a role="menuitem" tabindex="-1" href="#">pro Tag</a></li></ul></div></div></form>

This the ID which I got from the web addon from selenium:

"id": "f2a5983c-bdd9-4373-804b-af70cd7ad751",

"comment": "",

"command": "click",

"target": "css=.btn-primary",

"targets": [ ["css=.btn-primary", "css:finder"],

["xpath=(//button[@type='button'])[46]", "xpath:attributes"], ["xpath=//div[@id='dijit_layout__LayoutWidget_0']/section/div/section/div/div/div[3]/form/div /button", "xpath:idRelative"],

["xpath=//form/div/button", "xpath:position"],

["xpath=//button[contains(.,'Abwesenheit melden')]", "xpath:innerText"] ],

"value": ""

I just want to do an click on a singel button. I didn't understand why I cant click on a Button, I know that some attempts are for an older version but the older version didn't work neither.
those were my attempts:

 
element = driver.find_element("xpath", '//*[@id="f2a5983c-bdd9-4373-804b-af70cd7ad751"]')


element = driver.find_element_by_xpath("//div[@id='dijit_layout__LayoutWidget_0']/section/div/section/div/div/div[3]/form/div/button")
element.click()

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//button[@title="Abwesenheit melden" and @class="btn btn-primary"]'))).click()

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/div/div/section/div/section/div/div/div[3]/form/div/button[1]'))).click()


button = driver.find_element_by_xpath("//div[@id='dijit_layout__LayoutWidget_0']/section/div/section/div/div/div[3]/form/div/button")
#button.click()

答案1

得分: 1

以下是您要的翻译内容:

"The button-element you mentioned does not have an id, thus you cannot use an id with the xpath-selector. If you have only one button on the website, you could try the CSS selector:

element = driver.find_element(By.CSS_SELECTOR, '.btn-primary')

If you have more buttons on the page and the button you want to click is e.g. button number 2 try

element = driver.find_elements(By.CSS_SELECTOR, '.btn-primary')[1]

Edit:

Based on your updated HTML snippet I created the following minimal Website:

<html>
	<body>
		<form class="un-form form-inline">
			<div>
				<button type="button" title="Abwesenheit melden" class="btn btn-primary">Abwesenheit melden</button>
				<button type="button" title="Löschen" class="btn btn-danger">Löschen</button>
				<div class="dropup btn-group">
					<button id="8a4bb522-063d-4eca-a121-3f32933a3a1e" role="button" aria-haspopup="true" aria-expanded="false" type="button" class="dropdown-toggle btn btn-default">Entschuldigungsschreiben drucken <span class="caret"></span></button>
					<ul role="menu" class="dropdown-menu" aria-labelledby="8a4bb522-063d-4eca-a121-3f32933a3a1e">
						<li role="presentation" class=""><a role="menuitem" tabindex="-1" href="#">pro Stunde</a></li>
						<li role="presentation" class=""><a role="menuitem" tabindex="-1" href="#">pro Tag</a></li>
					</ul>
				</div>
			</div>
		</form>
	</body>
</html>

Using the following Python script, I was able to click the button "Abwesenheit melden":

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(options=options,
                          executable_path="Driver/chromedriver")
driver.get("file:///C:/Users/stefan/Desktop/selenium_test.html")

element = driver.find_element(By.CSS_SELECTOR, '.btn-primary')
element.click()
英文:

The button-element you mentioned does not have an id, thus you cannot use an id with the xpath-selector. If you have only one button on the website, you could try the CSS selector:

element = driver.find_element(By.CSS_SELECTOR, '.btn-primary')

If you have more buttons on the page and the button you want to click is e.g. button number 2 try

element = driver.find_elements(By.CSS_SELECTOR, '.btn-primary')[1]

Edit:

Based on your updated HTML snippet I created the following minimal Website:

<html>
	<body>
		<form class="un-form form-inline">
			<div>
				<button type="button" title="Abwesenheit melden" class="btn btn-primary">Abwesenheit melden</button>
				<button type="button" title="Löschen" class="btn btn-danger">Löschen</button>
				<div class="dropup btn-group">
					<button id="8a4bb522-063d-4eca-a121-3f32933a3a1e" role="button" aria-haspopup="true" aria-expanded="false" type="button" class="dropdown-toggle btn btn-default">Entschuldigungsschreiben drucken <span class="caret"></span></button>
					<ul role="menu" class="dropdown-menu" aria-labelledby="8a4bb522-063d-4eca-a121-3f32933a3a1e">
						<li role="presentation" class=""><a role="menuitem" tabindex="-1" href="#">pro Stunde</a></li>
						<li role="presentation" class=""><a role="menuitem" tabindex="-1" href="#">pro Tag</a></li>
					</ul>
				</div>
			</div>
		</form>
	</body>
</html>

Using the following Python script, I was able to click the button "Abwesenheit melden":

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(options=options,
                          executable_path="Driver/chromedriver")
driver.get("file:///C:/Users/stefan/Desktop/selenium_test.html")

element = driver.find_element(By.CSS_SELECTOR, '.btn-primary')
element.click()

huangapple
  • 本文由 发表于 2023年5月24日 19:24:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定