使用硒和BeautifulSoap进行Web剪贴
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用硒和BeautifulSoap进行Web剪贴相关的知识,希望对你有一定的参考价值。
下面是元素的检查
<div class="input-group ref-container ">
<input id="sys_display.incident.assignment_group" name="sys_display.incident.assignment_group" aria-labelledby="label.incident.assignment_group" type="search" autocomplete="off" autocorrect="off" value="PeopleSoft Reporting ONLY" ac_columns="u_full_name" data-type="ac_reference_input" data-completer="AJAXTableCompleter" data-dependent="" data-dependent-value="" data-ref-qual="" data-ref="incident.assignment_group" data-ref-key="null" data-ref-dynamic="false" data-name="assignment_group" data-table="sys_user_group" class="form-control element_reference_input " style="; " spellcheck="false" onfocus="if (!this.ac) addLoadEvent(function() var e = gel('sys_display.incident.assignment_group'); if (!e.ac) new AJAXTableCompleter(gel('sys_display.incident.assignment_group'), 'incident.assignment_group', '', ''); e.ac.onFocus();)" aria-required="true" role="combobox" aria-autocomplete="list" aria-owns="AC.incident.assignment_group">
<span class="ref_dynamic_placeholder">A new record with this value will be created automatically</span>
<span class="input-group-btn">
<button id="lookup.incident.assignment_group" name="lookup.incident.assignment_group" type="button" class="btn btn-default" title="Lookup using list" aria-haspopup="true" data-for="sys_display.incident.assignment_group" data-type="ac_reference_input" tabindex="-1" role="button" aria-label="Look up value for field: Assignment group" data-original-title="Lookup using list">
<span class="icon icon-search" aria-hidden="true">
</span>
</button>
</span>
</div>
如何将值:"PeopleSoft Reporting ONLY"
写入变量。
预先感谢
id=
属性进行选择。例如:txt = '''<div class="input-group ref-container "><input id="sys_display.incident.assignment_group" name="sys_display.incident.assignment_group" aria-labelledby="label.incident.assignment_group" type="search" autocomplete="off" autocorrect="off" value="PeopleSoft Reporting ONLY" ac_columns="u_full_name" data-type="ac_reference_input" data-completer="AJAXTableCompleter" data-dependent="" data-dependent-value="" data-ref-qual="" data-ref="incident.assignment_group" data-ref-key="null" data-ref-dynamic="false" data-name="assignment_group" data-table="sys_user_group" class="form-control element_reference_input " style="; " spellcheck="false" onfocus="if (!this.ac) addLoadEvent(function() var e = gel('sys_display.incident.assignment_group'); if (!e.ac) new AJAXTableCompleter(gel('sys_display.incident.assignment_group'), 'incident.assignment_group', '', ''); e.ac.onFocus();)" aria-required="true" role="combobox" aria-autocomplete="list" aria-owns="AC.incident.assignment_group"><span class="ref_dynamic_placeholder">A new record with this value will be created automatically</span><span class="input-group-btn"><button id="lookup.incident.assignment_group" name="lookup.incident.assignment_group" type="button" class="btn btn-default" title="Lookup using list" aria-haspopup="true" data-for="sys_display.incident.assignment_group" data-type="ac_reference_input" tabindex="-1" role="button" aria-label="Look up value for field: Assignment group" data-original-title="Lookup using list"><span class="icon icon-search" aria-hidden="true"></span></button></span></div>'''
soup = BeautifulSoup(txt, 'html.parser')
s = soup.select_one('#sys_display\.incident\.assignment_group')['value']
print(s)
打印:
PeopleSoft Reporting ONLY
与:相同
s = soup.find(id="sys_display.incident.assignment_group")['value'] print(s)
仅PeopleSoft Reporting,您可以使用以下解决方案之一:
- 使用
css_selector
:print(driver.find_element_by_css_selector("input[id^='sys_display'][name*='incident'][aria-labelledby$='assignment_group']").get_attribute("value"))
xpath
:print(driver.find_element_by_xpath("//input[starts-with(@id, 'sys_display')][contains(@name, 'incident')][contains(@aria-labelledby, 'assignment_group')]").get_attribute("value"))
但是,按照最佳实践提取/打印所需的文本,您需要为WebDriverWait引入visibility_of_element_located()
,并且可以使用以下Locator Strategies之一:
- 使用
CSS_SELECTOR
:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".data-row"))).get_attribute("value"))
XPATH
:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[@class='data-row']"))).get_attribute("value"))
注
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
更新使用方式:
- NoSuchElementException
- WebDriverWait 您面对TimeoutException
find_element_by_*
您面对的顶级内容
中,并且可能在<iframe>
中。现在处理NoSuchElementException,请按照this和this讨论。以上是关于使用硒和BeautifulSoap进行Web剪贴的主要内容,如果未能解决你的问题,请参考以下文章
使用 windows 剪贴板对多个自定义剪贴板进行编程 - 一些复杂性