首先,对不起我的英语。我来自布宜诺斯艾利斯,几周前我就开始学习Python了。此外,我对编程有非常基本的了解。到目前为止,我所能做到的只是从互联网上获取信息(在这件事上没有正式的教育 - 去年我正在研究会计)。
截至本文,我想在网页中找到一个元素,但我似乎无法做到正确。我甚至试图点击“空格”键 - 在这种情况下最简单的事情。
我想点击“确定”按钮。
我来自“Inspect element”:
<div class="alert" id="popup_content">
<div id="popup_message">No Pending Documents</div>
<div id="popup_panel">
<input id="popup_ok" type="button" value="OK">
</div>
</div>
print:我试过这5个代码:
from selenium.webdriver.common.action_chains import ActionChains
element_ok = driver.find_element_by_xpath("//input[@id='popup_ok']")
Action.Chains(driver).move_to_element(element_ok).perform()
element.click()
driver.find_element_by_xpath(".//*[@id='popup_ok']/div/input").click()
driver.find_element_by_css_selector(".button_main[value='OK']").click()
clear_button = driver.find_element_by_xpath("//input[@id='popup_panel'][@type='button']")
clear_button = driver.find_element_by_xpath("//form[@id='popup_ok']/input[1]")
import keyboard
keyboard.press_and_release('shift+s, space')
有人会帮我解决这个问题吗?
PS:我正在使用selenium(在“使用Python自动化无聊的东西。初学者的实用编程”一书中读取它。和chrome webdriver。
如果你知道元素的id试试:
driver.find_element_by_id('popup_ok').click()
根据您共享的HTML,单击带有文本的按钮,您必须引导WebDriverWait才能使元素可单击,您可以使用以下代码行:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='alert' and @id='popup_content']//div[@class='popup_panel']/input[@id='popup_ok' and @value='OK']"))).click()
有多种方法可以定位元素
1.使用id
ok_btn = driver.find_element_by_id("popup_ok")
2.使用xpath
ok_btn = driver.find_element_by_xpath("//input[@value='OK']")
要么
ok_btn = driver.find_element_by_xpath("//input[@id='popup_ok']")
我们来看看每种方法。
from selenium.webdriver.common.action_chains import ActionChains
element_ok = driver.find_element_by_xpath("//input[@id='popup_ok']")
Action.Chains(driver).move_to_element(element_ok).perform()
element.click()
这有两个问题。
首先,导入ActionChains
,然后尝试将其用作Action.Chains
。因为你没有导入任何名为Action
的东西,你可能会看到这个错误:
NameError: name 'Action' is not defined
删除额外的内部.
应该修复它:
ActionChains(driver).move_to_element(element_ok).perform()
其次,找到元素并将其保存到名为element_ok
的变量中。然而,然后,您尝试在名为click()
的变量上调用element
。因为您尚未使用该名称定义变量,所以您可能会看到以下错误:
NameError: name 'element' is not defined
调用element_ok.click()
应该修复它:
element_ok.click()
注意:您可能根本不需要使用ActionChains
。如果元素位于视口之外,您应该只需要告诉Selenium移动到元素,例如“在首屏下方”:
element_ok = driver.find_element_by_xpath("//input[@id='popup_ok']")
element_ok.click()
driver.find_element_by_xpath(".//*[@id='popup_ok']/div/input").click()
此XPath查询告诉Selenium:
id
为"popup_ok"
(A)的元素,<div>
(B),<input>
(C),然后这对您的HTML没有意义。与id
"popup_ok"
元素是<input>
,既没有孩子<div>
也没有孙子<input>
。
由于id
"popup_ok"
(A)的元素是你想要的<input>
,你可以简单地删除XPath查询的其余部分:
driver.find_element_by_xpath(".//*[@id='popup_ok']").click()
driver.find_element_by_css_selector(".button_main[value='OK']").click()
这个CSS选择器告诉Selenium查找并返回以下元素:
button_main
和value
的"OK"
(A)。你的按钮符合要求2,但不符合1.它没有类button_main
。 (没有类button_main
的元素。)
相反,您可以使用*
匹配任何元素,无论类名如何:
driver.find_element_by_css_selector("*[value='OK']").click()
但是,这不是一个很棒的CSS选择器。因为*
匹配任何元素,它有可能变慢。
相反,您可以通过标记进行匹配:
driver.find_element_by_css_selector("input[value='OK']").click()
clear_button = driver.find_element_by_xpath("//input[@id='popup_panel'][@type='button']")
clear_button = driver.find_element_by_xpath("//form[@id='popup_ok']/input[1]")
第一个XPath查询告诉Selenium:
<input>
元素a。有id
"popup_panel"
和b。有type
"button"
。您的按钮符合要求1和1b,但不符合1a。 id
"popup_panel"
由祖父母<div>
使用。
要修复它,您可以删除该要求:
clear_button = driver.find_element_by_xpath("//input[@type='button']")
第二个XPath查询告诉Selenium:
<form>
元素(A)a。有id
"popup_ok"
,然后<input>
(B)。你的按钮符合要求2,但不符合1.它的父母既不是<form>
标签,也没有id
的"popup_ok"
。
相反,<input>
有id
:
clear_button = driver.find_element_by_xpath("//input[@id='popup_ok'][1]")
注意:[1]
选择第一个匹配元素。由于id
属性应该是唯一的,这是多余的,可以删除:
clear_button = driver.find_element_by_xpath("//input[@id='popup_ok']")
import keyboard
keyboard.press_and_release('shift+s, space')
这可能不是很多原因,可能不值得进入。 Selenium能够模拟键盘事件,因此您可能不需要使用其他Python包。
如果你有接收键盘事件的元素,你可以在上面调用send_keys()
:
from selenium.webdriver.common.keys import Keys
element.send_keys(Keys.SPACE)
如果您没有该元素,则可以使用ActionChains
将键盘事件发送到当前活动元素:
ActionChains(driver).send_keys(Keys.SPACE).perform()