使用Selenium,为什么以下函数会打印空字符串?如果我尝试'innerText'
而不是'innerHTML'
,则相同。
send_keys
部分工作正常。
def button_clicked(self):
browser = webdriver.Firefox()
browser.get('https://www.google.com')
search_box = browser.find_element_by_xpath("//input[@title='Search']")
search_box_HTML = search_box.get_attribute('innerHTML')
print(search_box_HTML)
答案 0 :(得分:2)
<input>
没有内部内容文字或HTML。相反,用户输入的数据存储在value
属性中,可以使用WebElement#get_attribute()
检索该属性:
search_box.get_attribute(“value”)
答案 1 :(得分:0)
您应该将代码更改为:
search_box.get_attribute('value')
应该做的伎俩
答案 2 :(得分:0)
如果查看页面https://www.google.com
的 HTML 并检查 搜索框 WebElement 您已确定为:
find_element_by_xpath("//input[@title='Search']")
WebElement 的定义如下:
<input class="gsfi" id="lst-ib" maxlength="2048" name="q" autocomplete="off" title="Search" type="text" value="" aria-label="Search" aria-haspopup="false" role="combobox" aria-autocomplete="list" dir="ltr" spellcheck="false" style="border: none; padding: 0px; margin: 0px; height: auto; width: 100%; background: url("data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D") transparent; position: absolute; z-index: 6; left: 0px; outline: none;">
get_attribute("innerHTML")
强> 根据文档,Element.innerHTML获取描述元素后代的HTML语法,get_attribute()
定义如下:
def get_attribute(self, name):
"""Gets the given attribute or property of the element.
This method will first try to return the value of a property with the
given name. If a property with that name doesn't exist, it returns the
value of the attribute with the same name. If there's no attribute with
that name, ``None`` is returned.
Values which are considered truthy, that is equals "true" or "false",
are returned as booleans. All other non-``None`` values are returned
as strings. For attributes or properties which do not exist, ``None``
is returned.
:Args:
- name - Name of the attribute/property to retrieve.
Example::
# Check if the "active" CSS class is applied to an element.
is_active = "active" in target_element.get_attribute("class")
"""
因此,由于 WebElement 即 搜索框 没有后代,所以 get_attribute('innerHTML')
返回为空