簡體   English   中英

<a>通過 -- data-slide=&quot;next&quot; 使用 Selenium 和 Python 單擊按鈕</a>

[英]Clicking an <a> button using Selenium and Python with -- data-slide="next"

我不熟悉在 Python 上使用 Selenium 進行網頁抓取,並且在瀏覽網頁時遇到問題。 我當前的代碼通過使用以下按鈕單擊按鈕瀏覽網頁:

web.find_element("xpath", '').click()

在單括號中使用適當的 xpath。

我正在嘗試“單擊”其 HTML 如下所示的按鈕:

<a id="medicalUsageNext" href="#myPereferences" data-slide="next" class="btn btn-primary pull-right margin10-b gtm_medical_service_next" title="Next" role="button">

 Next
 <i class="icon-caret-right"></i>
 </a> 

盡管嘗試單擊此按鈕的方法有多種(添加隱式等待、按 css、文本或類型選擇),但我仍會收到 ElementNotInteractableException。

這是我嘗試單擊按鈕的一種(多種)方法:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

web = webdriver.Chrome()
web.implicitly_wait(10)
web.get('https://apply.coveredca.com/lw-shopandcompare/?lang=en_EN')

zip2 = "90210"
income2 = "100000"
year_1 = "2022" 
household_1 = "1"
subsidy_household_size = "2"

web.find_element("xpath", '//*[@id="screeningquestions-zip"]').send_keys(zip2)
web.find_element("xpath", '//*[@id="screeningquestions- 
householdincomeperyear"]').send_keys(income2)

year = web.find_element("xpath", '//*[@id="screeningquestions-enrollyear"]')
year.send_keys(year_1)
year.send_keys(Keys.RETURN)

hh = web.find_element("xpath", '//*[@id="screeningquestions-howmanypeopleinhousehold"]')
hh.send_keys(subsidy_household_size)
hh.send_keys(Keys.RETURN)

for x1 in range(0, int(household_1)):
    temp1 = str(x1)
    web.find_element("xpath", '//*[@id="screeningquestions-age-'+temp1+'"]').send_keys("33")

for x2 in range(int(household_1), int(subsidy_household_size)):
    temp2 = str(x2)
    web.find_element("xpath", '//*[@id="screeningquestions-age-'+temp2+'"]').send_keys("35")
    temp3 = str(x2+1)
    web.find_element("xpath", '//*[@id="app-container"]/main/div/div/div[2]/div/div/div/form/span[3]/div['+temp3+']/div[3]/div[1]/label/span[1]').click()

web.find_element("xpath", '//*[@id="account-creation-access-code-invalid-modal-button"]').click()
web.find_element("xpath", '/html/body/div[3]/div/div/div/div[2]/div/div[1]/button').click()

web.find_element("xpath", '//*[@id="shopandcompare-previewplans"]').click()
web.find_element("xpath", '//*[@id="providerSearchNext"]').click()
web.find_element("xpath", '//*[@id="medicalUsageNext"]').click

作為參考,請點擊以下網站: https ://apply.coveredca.com/lw-shopandcompare/?lang=en_EN

幾天來我一直在努力解決這個問題,非常感謝任何幫助。

要單擊文本為Next的元素,您需要為element_to_be_clickable()引入WebDriverWait ,您可以使用以下任一定位器策略

  • 使用PARTIAL_LINK_TEXT

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Next"))).click()
  • 使用CSS_SELECTOR

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#medicalUsageNext[data-slide='next'][title='Next']"))).click()
  • 使用XPATH

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@id='medicalUsageNext' and @data-slide='next'][@title='Next']"))).click()
  • 注意:您必須添加以下導入:

     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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM