0

I'm trying to automate browsing through several pages of lists of doctors. The part I'm having difficulty with is how to get selenium to find and click on the right hand arrow that goes to the next pages of 10 doctors.

I've been trying several different stackOverflow potential solutions for the past few days and I'm still stumped.

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()

Any suggestions or feedback would really be appreciated!

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

3 Answers3

2

To click() on the desired element you have to induce WebDriverWait for the desired element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using 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")))
    
  • Using 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']")))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • @Madder12358 I saw you had several code trials which was useful for analysis. Can you try once with my solution and get back to me with the status please? – undetected Selenium Jun 08 '19 at 22:20
0

The following worked for me -

next_page_btn = browser.find_element_by_xpath("//button[@track='next-page']")
next_page_btn.click()
time.sleep(2)
LuckyZakary
  • 1,171
  • 1
  • 7
  • 19
0

First check element visible on the page or not after that click on that element Here is the example code:

WebDriverWait wait= new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOf("path of the element"));
browser.find_element_by_xpath("path of the element").click();