Cannot click on element - selenium

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!

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

The following worked for me -
next_page_btn = browser.find_element_by_xpath("//button[#track='next-page']")
next_page_btn.click()
time.sleep(2)

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

Related

Get link and title of sub link in a homepage

I want to get a link inside the homepage of the website,
I tried to get it by selenium but only get the homepage link.
please help me to get all links inside.
Code trials:
from selenium import webdriver
import time
driver = webdriver.Edge()
driver.get('https://m.cafe.naver.com/ca-fe/minivelobike')
time.sleep(7)
elems = driver.find_elements_by_xpath("//a[#href]")
for elem in elems:
print(elem.get_attribute("href"))
driver.close()
Snapshot of the links:
To print the value of the href attributes of the articles you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
driver.get("https://m.cafe.naver.com/ca-fe/minivelobike")
print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "a.txt_area[href]")))])
Using XPATH:
driver.get("https://m.cafe.naver.com/ca-fe/minivelobike")
print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//a[#class='txt_area' and #href]")))])
Console Output:
['https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074719&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074718&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074717&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074716&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074715&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074714&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074713&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074712&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074711&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074710&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074709&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074708&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074707&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074704&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074703&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074702&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074701&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074700&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074699&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074698&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074697&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074696&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074695&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074694&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074693&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074692&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074691&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074690&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074689&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074688&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074687&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074686&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074685&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074682&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074681&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074680&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074679&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074678&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074677&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074676&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074675&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074674&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074673&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074672&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074671&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074670&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074669&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074668&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074667&boardtype=L', 'https://m.cafe.naver.com/ArticleRead.nhn?clubid=11853711&articleid=1074666&boardtype=L']
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
i think this should work
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Edge()
driver.get('https://m.cafe.naver.com/ca-fe/minivelobike')
time.sleep(7)
wrapper = driver.find_element(By.ID, "ct")
main_ul = wrapper.find_element(By.TAG_NAME, "ul")
for li in main_ul.find_elements(By.TAG_NAME, "li"):
try:
anchor_tag = li.find_element(By.TAG_NAME, "a")
href = anchor_tag.get_attribute("href")
print(href)
except:
print("Anchor tag doesnt exist")

Selenium won't click on all buttons only a few

I'm trying to click the buttons on the left-panel of the web-page I'm trying to scrape. However, by using Selenium, it seems to only click only on a few of these buttons. I have added a time.sleep between each click which did not make a difference.
I just get the following error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"(//div[#class='toggle-bottom-filter'])[7]"}
I have double checked that path and it does exist on the website. So I'm not certain as to why it's undetectable, any ideas?
Here's my script:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome()
driver.get("https://www.theparking.eu/#!/used-cars/")
wait=WebDriverWait(driver,15)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#bloc-filter")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div[class='sd-cmp-25TOo'] span[class='sd-cmp-16t61 sd-cmp-2JYyd sd-cmp-3cRQ2']"))).click()
#WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='Electric car drivers will soon no longer be able to charge their cars here.']")))
stuff = []
for more in range(1, 9):
time.sleep(2)
driver.find_element(By.XPATH, f"(//div[#class='toggle-bottom-filter'])[{more}]").click()
data = driver.page_source
# ... parse with beautifulsoup
Not all the 9 elements you are trying to click are initially visible, you first will have to scroll the element into the view and only after that click them.
I see there are 11 elements matching //div[#class='toggle-bottom-filter'] locator there so possibly you should change the for loop to for more in range(1, 12):
I think the following code should work better:
from selenium import webdriver
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.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome()
driver.get("https://www.theparking.eu/#!/used-cars/")
wait=WebDriverWait(driver,15)
actions = ActionChains(driver)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#bloc-filter")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div[class='sd-cmp-25TOo'] span[class='sd-cmp-16t61 sd-cmp-2JYyd sd-cmp-3cRQ2']"))).click()
#WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='Electric car drivers will soon no longer be able to charge their cars here.']")))
stuff = []
for more in range(1, 12):
time.sleep(2)
button = driver.find_element(By.XPATH, f"(//div[#class='toggle-bottom-filter'])[{more}]")
actions.move_to_element(button).perform()
time.sleep(0.5)
button.click()
time.sleep(0.5)
data = driver.page_source
# ... parse with beautifulsoup

How to select the right size of a product? Selenium - ChromeDriver

My Code:
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
browser = webdriver.Chrome('C:/Users/rober/OneDrive/Skrivebord/bot/chromedriver')
# Graffik kort
browser.get("https://www.zalando.dk/jordan-air-jordan-1-mid-sneakers-high-joc12n001-a18.html")
buyButton = False
while buyButton is False:
try:
addToCartBtn = addButton = browser.find_element_by_xpath('/html/body/div[4]/div/div[2]/div/div/div[2]/div[1]/x-wrapper-re-1-6/div/div[4]/button')
print("Varen er udsolgt")
time.sleep(1)
browser.refresh()
except:
addToCartBtn = addButton = browser.find_element_by_xpath('//*[#id="picker-trigger"]')
print("Varen er på Lager")
buyButton = True
while buyButton is True:
time.sleep(3)
accept = browser.find_element_by_id('uc-btn-accept-banner')
browser.execute_script("arguments[0].click();", accept)
element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="picker-trigger"]')))
element.click();
My Error:
Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="uc-btn-accept-banner"]"}
The link to the Website
My problem is that i want to get the size 51.5 in eu sizes but i don't know the code to select that size with the CLASS of the size.
If some one know a solution, just make a comment because i really need some help to find the right solution.
To select the size as 51.5 you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies:
Code Block:
driver.get("https://www.zalando.dk/jordan-air-jordan-1-mid-sneakers-high-joc12n001-a18.html")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.uc-btn#uc-btn-accept-banner"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Vælg størrelse']"))).click()
driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//label[starts-with(#for, 'size-picker')]//span[text()='51.5']"))))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[starts-with(#for, 'size-picker')]//span[text()='51.5']"))).click()
Browser Snapshot:
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

I want execute button.click() operation once finds value more than "+1.00%" in a webpage using Selenium, python

Guy's please help, I want execute an element.click() operation once element finds an text in a web page more than "+1.00%" value. Below is my code. The code will repeat until finds text more than "+1.00%"
def main():
while True:
try:
element = browser.find_element_by_link_text('+1.00%')
element2 = browser.find_element_by_link_text('Balances')
if element > '+1.00%':
element2.click()
time.sleep(5)
except:
main()
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
driver.get('url')
WebDriverWait(driver, 10000).until(
EC.presence_of_element_located(
(By.XPATH, '//*[contains(text(),"+1.50")]')
)
).click()
use webdriver wait instead,

Unable to locate element: Selenium

I want to select the More Information link by clicking on it. I have tried everything I could but every time the error NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector" pops up.
At first, I thought maybe because I did not change the tabs properly that's why this error is showing. But, even after using window_handles still I cannot locate any element on this page.
Please HELP!
self.driver.window_handles
base = self.driver.window_handles[0]
child = self.driver.window_handles[1]
window_set = {self.driver.window_handles[0], self.driver.window_handles[1]}
for x in window_set:
if(base != x):
self.driver.switch_to.window(x)
self.driver.find_element_by_id("mc-lnk-moreInfo").click()
Please check below solution using contains and ID
Xpath with contains
element= WebDriverWait(self.driver, 30).until(
EC.element_to_be_clickable((By.XPATH, '//*[contains(text(), 'More information')]')))
self.driver.execute_script("arguments[0].click();", element)
or
Xpath with ID
element= WebDriverWait(self.driver, 30).until(
ec.element_to_be_clickable((By.ID, "//a[#id='mc-lnk-moreInfo']")))
self.driver.execute_script("arguments[0].click();", element)
Working Solution :
driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
driver.get("your url")
childframe = wait.until(EC.presence_of_element_located((By.NAME, "mainFrame")))
driver.switch_to.frame(childframe)
element=wait.until(EC.element_to_be_clickable((By.ID, "mc-lnk-moreInfo")))
print element.text
element.click()
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
Output:
Try to wait for the element then click on it
Replace
self.driver.find_element_by_id("mc-lnk-moreInfo").click()
With following
self.more_info = WebDriverWait(self.driver, 30).until(
ec.visibility_of_element_located((By.ID, "//a[#id='mc-lnk-moreInfo']")))
ActionChains(self.driver).move_to_element(self.more_info).click().perform()
add the following on with your imports
from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec