如果希望创建一个通用代码(使用Selenium),该代码将查找标签,并在标签输入(或选择)标记旁边查找并插入值
主要功能:
for l in label:
try:
xpathInput = "//label[contains(.,'{}')]/following::input".format(l)
checkXpathInput, pathInput= check_xpath(browser,xpathInput)
if checkXpathInput is True:
pathInput.clear()
pathInput.send_keys("\b{}".format(value))
break
for op in option:
xpathSelect = "//label[contains(.,'{}')]/following::select/option[text()='{}']".format(l,op)
checkXpathSelect, pathSelect= check_xpath(browser,xpathSelect)
if checkXpathSelect is True:
pathSelect.click()
break
except:
print("Can't match: {}".format(l))
路径检查器:
def check_xpath(browser,xpath):
try:
path = browser.find_element_by_xpath(xpath)
except NoSuchElementException:
return False
return True , path
当前的问题是什么
input
标记,然后他将去检查标签“标题”和e.t.c.旁边是否有select
标记李>在我的当前输入中,他将找到标签“Title”,然后将值填充到下一个输入中(这是不正确的,因为“Title”使用的是SELECT标记)
我会利用
find_elements_by_xpath
返回已找到元素的列表,而空列表是错误的这一事实。因此,您不需要try/except和返回bool
或tuple
值(这不是最佳行为)的函数用一些html源代码示例给出一个好的答案会更容易,但我假设您想做的是:
我不知道你使用的页面有多整洁,但是如果做得好,那么你的标签应该有一个
for="something"
属性。如果是这种情况,那么您只需查找与标签相关的元素,并确定其标记是否为input(或select):相关问题 更多 >
编程相关推荐