无法使用 Selenium 包提取属性
Unable to extract an attribute using the Selenium package
总体思路是使用 Selenium 包提取 DOM。
具体来说,objective是提取max
中驻留在classpagination__input hide-phone
中的值,如图以下
<div class="pagination__input hide-phone">
<input type="number" name="page" max="12" value="1">
</div>
可从此link访问该网站。
为此,起草了以下代码。
maxPage = self.browser.find_elements_by_css_selector( "[class='pagination__input hide-phone']" )
valueMax=maxPage.get_attribute( 'max' )
但是,抛出以下错误。
AttributeError: 'list' object has no attribute 'get_attribute'
请问是什么问题。
提前致谢。
编辑 2
根据@Kunduk 的建议。
按照@Kunduk 的说法,以下行已相应调整。具体来说,添加 self.
print( WebDriverWait( self.browser, 10 ).until( EC.visibility_of_element_located(
(By.CSS_SELECTOR, "div.pagination__input.hide-phone>input[name='page']") ) ).get_attribute( "max" ) )
但是,我得到了以下错误
编辑 1:
如@Josh 所建议
pages = self.browser.find_elements_by_css_selector( "[class='pagination__input hide-phone']" )
maxPages = [page for page in pages if page.get_attribute('max')]
但是return是[]
的结果,也如下图所示:
要获取页面的最大值,请引入 WebDriverWait
() 并等待 presence_of_element_located
() 并跟随 css selector
。
driver.get("https://www.freepik.com/search?dates=any&format=search&page=1&query=Polygonal%20Human&sort=popular")
print(WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,"div.pagination__input.hide-phone>input[name='page']"))).get_attribute("max"))
您需要导入以下库。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
快照:
使用这个选择器 div.pagination__input > input
并引入 WebDriverWait()
:
driver.get('https://www.freepik.com/search?dates=any&format=search&page=1&query=Polygonal%20Human&sort=popular')
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.pagination__input > input')))
print(element.get_attribute('max'))
总体思路是使用 Selenium 包提取 DOM。
具体来说,objective是提取max
中驻留在classpagination__input hide-phone
中的值,如图以下
<div class="pagination__input hide-phone">
<input type="number" name="page" max="12" value="1">
</div>
可从此link访问该网站。
为此,起草了以下代码。
maxPage = self.browser.find_elements_by_css_selector( "[class='pagination__input hide-phone']" )
valueMax=maxPage.get_attribute( 'max' )
但是,抛出以下错误。
AttributeError: 'list' object has no attribute 'get_attribute'
请问是什么问题。 提前致谢。
编辑 2
根据@Kunduk 的建议。
按照@Kunduk 的说法,以下行已相应调整。具体来说,添加 self.
print( WebDriverWait( self.browser, 10 ).until( EC.visibility_of_element_located(
(By.CSS_SELECTOR, "div.pagination__input.hide-phone>input[name='page']") ) ).get_attribute( "max" ) )
但是,我得到了以下错误
编辑 1:
如@Josh 所建议
pages = self.browser.find_elements_by_css_selector( "[class='pagination__input hide-phone']" )
maxPages = [page for page in pages if page.get_attribute('max')]
但是return是[]
的结果,也如下图所示:
要获取页面的最大值,请引入 WebDriverWait
() 并等待 presence_of_element_located
() 并跟随 css selector
。
driver.get("https://www.freepik.com/search?dates=any&format=search&page=1&query=Polygonal%20Human&sort=popular")
print(WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,"div.pagination__input.hide-phone>input[name='page']"))).get_attribute("max"))
您需要导入以下库。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
快照:
使用这个选择器 div.pagination__input > input
并引入 WebDriverWait()
:
driver.get('https://www.freepik.com/search?dates=any&format=search&page=1&query=Polygonal%20Human&sort=popular')
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.pagination__input > input')))
print(element.get_attribute('max'))