如何从脚本中的目标页面获得“名字”。我尝试了如下所示,但是它引发了以下错误:
"selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression "//div[@class="div_input_place"]/input[@id="txt_name"]/@value" is: [object Attr]. It should be an element."但是,“Name”在其中所指的元素是:
<div class="div_input_place">
                                                <input name="txt_name" type="text" value="CLINTO KUNJACHAN" maxlength="20" id="txt_name" disabled="disabled" tabindex="2" class="aspNetDisabled textboxDefault_de_active_student">
                                            </div>到目前为止我尝试过的脚本:
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.icaionlineregistration.org/StudentRegistrationForCaNo.aspx")
driver.find_element_by_id('txtRegistNo').send_keys('SRO0394294')
driver.find_element_by_id('btnProceed').click()
time.sleep(5)
name = driver.find_element_by_xpath('//div[@class="div_input_place"]/input[@id="txt_name"]/@value')
print(name.text)
driver.quit()发布于 2017-06-19 17:48:48
Selenium不支持这种语法。XPath表达式应该只返回WebElement,而不是属性值或文本。试着使用下面的代码:
name = driver.find_element_by_xpath('//div[@class="div_input_place"]/input[@id="txt_name"]').get_attribute('value')
print(name)发布于 2017-06-19 17:49:01
不能以Selenium中的XPaths作为目标属性--表达式必须始终与实际元素匹配:
name_element = driver.find_element_by_xpath('//div[@class="div_input_place"]/input[@id="txt_name"]')
name_attribute = name_element.get_attribute("value")
print(name_attribute)请注意,我还切换到了一个更简洁、更易读的CSS选择器:
driver.find_element_by_css_selector('.div_input_place input#txt_name')或者,如果您的id是唯一的,甚至可以使用"find“:
driver.find_element_by_id("txt_name")https://stackoverflow.com/questions/44636853
复制相似问题