[英]How to click the buttons A to Z using Selenium and Python within the webpage http://architects-register.org.uk/list/regions
http://architects-register.org.uk/list/regions
所以我有這個網站,我希望能夠讓代碼點擊按鈕 A,然后是 B 和 C 等等。 我該怎么做。 我嘗試了這段代碼,它試圖獲取元素子元素的值,然后相應地單擊按鈕,但它不起作用。 我懷疑除了下面的錯誤之外還有其他一些問題。
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
url = 'http://architects-register.org.uk/'
driver.get(url)
find_uk = driver.find_element_by_id('ctl00_hlCountiesT').click()
elements = alphabets[0].find_element_by_tag_name('li')
def getChild(el):
return el.children[0].children[0]
for element in elements:
element = element[elements]
(getChild(element)).click()
NameError: name 'alphabets' is not defined
沒有在給定代碼塊中的任何地方導入或定義alphabets
,因此嘗試訪問其上的索引將按所示方式失敗。 看起來alphabets
應該可能是調用find_elements_by_tag_name
或find_elements_by_class_name
的結果,這將返回您的按鈕列表,然后您可以遍歷並單擊該列表。
誘導WebDriverWait
和visibility_of_all_elements_located
() 然后點擊每個鏈接。
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
driver = webdriver.Firefox()
url = 'http://architects-register.org.uk/'
driver.get(url)
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"ctl00_hlCountiesT"))).click()
elements =WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.XPATH,"//div[@class='alphabet_list_content']//li//input")))
for ele in range(len(elements)):
elements = WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='alphabet_list_content']//li//input")))
elements[ele].click()
要單擊按鈕A ,然后單擊B和C等等,您必須為element_to_be_clickable()
誘導WebDriverWait並且您可以使用以下任一定位器策略:
使用CSS_SELECTOR
:
driver.get("http://architects-register.org.uk/list/regions") for i in range(1, len(WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.alphabet_list_content>ul li"))))): WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.alphabet_list_content>ul li:nth-of-type({})>a>input".format(i)))).click() driver.quit()
使用XPATH
:
driver.get("http://architects-register.org.uk/list/regions") for i in range(1, len(WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='alphabet_list_content']/ul//li"))))): WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='alphabet_list_content']/ul//following-sibling::li[{}]/a/input".format(i)))).click() driver.quit()
注意:您必須添加以下導入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.