使用css选择器单击按钮的Python代码



我是python的新手,正在尝试修改一些现有的代码。我有一个包含以下元素的表格:

<div class="v-actions">
<input type="submit" value="Save"/>
<input type="button" value="Accept"/>
<input type="button" value="Reject"/>
</div>

如果它存在,我想单击Accept按钮。如果没有,我想记录它没有找到。目前,代码是这样的:

from bs4 import BeautifulSoup
import datetime
import getpass
from gmail import Gmail
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from time import sleep
....
self.chrome_session = webdriver.Chrome()
....
links = self.chrome_session.find_elements_by_class_name('link-record')
links = [(link.text, link.get_attribute('href').decode('utf-8'))
for link in links]
if len(links) == 0:
print("No work orders available at {0}".format(
datetime.datetime.now()))
else:
for link_text, link_url in links:
print("Clicking work order {0}".format(link_text))
self.chrome_session.get(link_url)
potential_input = self.chrome_session.find_element_by_class_name('v-actions').find_element_by_tag_name('input');
if potential_input.get_attribute('value') == 'Accept':
potential_input.click()
print("Accepted work order {0} at {1}.".format(link_text,datetime.datetime.now()))
else:
print("Accept input not found.")
....

在jquery中,选择器类似于:

div.v-actions input[value=Accept]

我不知道如何用Python实现这一点。我宁愿不把所有的输入都循环一遍。我只想知道具体的输入是否存在,如果存在,就接受它。

如何将css选择器转换为Python?

更新

我现在正在尝试这个:

potential_input = self.chrome_session.find_element_by_xpath("//div[@class='v-actions']/input[@value='Accept']")
if potential_input.is_displayed():
print("Accept input not found")
else:
potential_input.click()
print("Accepted work order {0} at {1}.".format(link_text,
datetime.datetime.now()))

我不知道如何测试它是否找到了元素。我得到这个错误:

selenium.common.exceptions.ElementNotVisibleException: Message: {"errorMessage":"Element is not currently visible and may not be manipulated","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"81","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:49795","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{"sessionId": "7b58b250-ca34-11e7-967d-6fd95b435952", "id": ":wdc:1510771259080"}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/7b58b250-ca34-11e7-967d-6fd95b435952/element/:wdc:1510771259080/click"}}

我不确定我的选择器是否错误,或者我的测试是否不好。

更新2

我已将代码修改为以下内容:

for link_text, link_url in links:
print("Clicking work order {0}".format(link_text))
self.chrome_session.get(link_url)
try:
self.chrome_session.find_element_by_xpath("//input[@value='Accept']").click()
print("Accepted work order {0} at {1}.".format(link_text,datetime.datetime.now()))
except ElementNotVisibleException:
print("Accept input not found")
self.chrome_session.back()

现在代码执行了,它的行为就像工作一样(它打印Accepted work order {0}),但它没有正确地激发.click(),因为记录仍然存在于列表中。但是,如果我手动单击"接受",它将正确处理作业。

我的选择器还是错的吗?或者,点击功能是否应用错误?

使用XPath选择您的输入,如果您试图选择不存在/不可见的内容,请请求原谅,以表现出Python风格:

from selenium.common.exceptions import ElementNotVisibleException
try:
potential_input = driver.find_element_by_xpath("//div[@class='v-actions']/input[@value='Accept']")
print("Input found!")
potential_input.click()
print("Input clicked!")
except ElementNotVisibleException:
# Asked for forgiveness for
# attempting to select element
# that isn't there/visible
print("Input not there!")

源于Selenium XPath和Exceptions文档。

最新更新