Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用于查找maxlength属性值的Xpath或Css选择器_Python_Selenium - Fatal编程技术网

Python 用于查找maxlength属性值的Xpath或Css选择器

Python 用于查找maxlength属性值的Xpath或Css选择器,python,selenium,Python,Selenium,我想使用xpath或css选择器从以下html中提取maxlength属性的值 <div class="qty-box" data-reactid=".8.0.0"><input class="qty-box-input" id="qtyMainItems" title="quantity box" type="text" value="1" maxlength="3" data-reactid=".8.0.0.0" style=""><butt

我想使用xpath或css选择器从以下html中提取maxlength属性的值

<div class="qty-box" data-reactid=".8.0.0"><input class="qty-box-input" 
   id="qtyMainItems" title="quantity box" type="text" value="1" 
     maxlength="3" data-reactid=".8.0.0.0" style=""><button type="button" 

对xpath执行类似的操作

from selenium.webdriver.common.by import By

element = driver.find_element(By.XPATH, '//*[@id="qtyMainItems"]')
element.get_attribute('maxlength')
如果它不是一个非常复杂的结构,那么您可能根本不需要担心xpath。相反,您可以直接搜索具有给定id的元素,如下所示:

element = driver.find_element_by_id('qtyMainItems')
element.get_attribute('maxlength')

我不懂Python,但是xml中没有class
.maxlength
,这是一个属性。谢谢。这确实是我一直在寻找的
element = driver.find_element_by_id('qtyMainItems')
element.get_attribute('maxlength')