我正在尝试自动浏览医生名单的几页。我遇到的困难是如何获取硒,然后单击右箭头,该箭头会转到10位医生的下一页。
在过去的几天里,我一直在尝试几种不同的stackOverflow潜在解决方案,但我仍然感到困惑。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
# from selenium.webdriver.common import move_to_element
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.actions import interaction
import time
import sys
browser = webdriver.Chrome('C:/chromedriver.exe')
browser.get('https://connect.werally.com/county-plan-selection/uhc.mnr/zip')
elem_ZipInput = browser.find_element_by_xpath('//*[@id="location"]')
elem_ZipInput.click()
elem_ZipInput.send_keys('80210')
elem_ZipInput.send_keys(Keys.ENTER)
time.sleep(2)
browser.find_element_by_xpath("//button[@track='No Preference']").click()
time.sleep(3)
browser.find_element_by_xpath("//button[@data-test-id='People']").click()
time.sleep(2)
try:
browser.find_element_by_xpath("//button[@data-test-id='Primary Care']").click()
except:
browser.find_element_by_xpath("//button[@data-test-id='PrimaryCare']").click()
time.sleep(2)
try:
browser.find_element_by_xpath("//button[@data-test-id='All Primary Care Physicians']").click()
except:
browser.find_element_by_xpath("//button[@data-test-id='AllPrimaryCarePhysicians']").click()
time.sleep(2)
elem_PCPList_NextPage = browser.find_element_by_xpath("//i[@class='icon icon_arrow_right']")
ProviderPageTab_Overview = browser.find_element_by_xpath("//*[@id='provider.bioTab']")
ProviderPageTab_Overview.click()
time.sleep(2)
# WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//compare-providers[@class='navigationHeader visible-phone']/div/div/button[@track='next-page']/icon/i"))).click()
# WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div[id='mainContent'] div div header compare-providers[class='navigationHeader visible-phone'] div div button[track='next-page']"))).click()
# WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div[id='mainContent'] div div header div[class='navigationHeader hidden-phone'] div div button[track='next-page'] icon"))).click()
wait = WebDriverWait(webdriver, 10)
wait.until(EC.element_to_be_clickable(By.CSS_SELECTOR,"div[id='mainContent'] div div header div[class='navigationHeader hidden-phone'] div div button[track='next-page'] icon"))
# print(browser.find_element_by_xpath("//i[@class='icon icon_arrow_right']"))
# print(browser.find_element_by_xpath("//button[@aria-label='Next Page']"))
next_Provider = browser.find_element_by_xpath("//compare-providers[@class='navigationHeader visible-phone']/div/div/button[@track='next-page']/icon/i")
#print(//compare-providers[@class='navigationHeader visible-phone']/div/div/button[@track='next-page']/icon/i)
# print(browser.find_element_by_xpath("//button[@track='next-page']"))
# print(browser.find_element_by_xpath("//icon[@type=\"'icon_arrow_right'\"]"))
next_Provider.click()
任何建议或反馈将不胜感激!
答案 0 :(得分:2)
要对所需元素进行click()
,必须为所需element_to_be_clickable()
引入 WebDriverWait ,并且可以使用以下Locator Strategies之一:
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[track='next-page'][aria-label='Next Page'] i.icon.icon_arrow_right")))
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@track='next-page' and @aria-label='Next Page']//i[@class='icon icon_arrow_right']")))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
答案 1 :(得分:0)
以下内容对我有用-
next_page_btn = browser.find_element_by_xpath("//button[@track='next-page']")
next_page_btn.click()
time.sleep(2)
答案 2 :(得分:0)
在页面上可见或不可见的第一个检查元素,然后单击该元素 这是示例代码:
WebDriverWait wait= new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOf("path of the element"));
browser.find_element_by_xpath("path of the element").click();