scroll google map webpage sidebar using selenium and python - python-3.x

I am trying to collect data on a google map webpage, this is the link. link
This is the code I have tried. My idea is to scroll to the "website name" (you can find the website name once you scroll down) once is present in the browser. but it is not scrolling.
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.maximize_window()
driver.get("https://www.google.com/maps/place/Amsterdamsche+Athleten+Club+Hercules/#52.36937,4.8049968,16.25z/data=!4m5!3m4!1s0x47c5e3c9cbb3d913:0xef85f93ef996cc06!8m2!3d52.3692292!4d4.8056684")
img_result = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH,'//*[#id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div/div/div[7]/div[5]/a/div[1]/div[2]/div[1]')))
driver.execute_script("arguments[0].scrollIntoView(true);",img_result)
print(img_result.text)
driver.close()
what is the solution for this?
EDIT:This is what I'm trying to get.

At least on my side I see no need to scroll.
The following code worked:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 20)
url = "https://www.google.com/maps/place/Amsterdamsche+Athleten+Club+Hercules/#52.36937,4.8049968,16z/data=!4m5!3m4!1s0x47c5e3c9cbb3d913:0xef85f93ef996cc06!8m2!3d52.3692292!4d4.8056684"
driver.get(url)
name = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "[data-item-id='authority']"))).text
print(name)
Output:
aachercules.nl
The same could be done with XPath instead of CSS Selectors.
This is the XPath I used:
name = wait.until(EC.visibility_of_element_located((By.XPATH, "//a[#data-item-id='authority']"))).text

Related

Can't click on 'Accept' cookies, how to find correct frame?

I am trying to click on 'Alles accepteren' (in english: Accept all) in the cookie pop-up but nothing seems to work. It probably has to do with focusing on the right window. Any help would be appreciated.
My code:
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path="path to chromedriver.exe")
driver.maximize_window()
driver.get("https://www.hornbach.nl/")
wait = WebDriverWait(driver,30)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='??']")))
cookie = wait.until(EC.element_to_be_clickable((By.XPATH,"//button[text()='Alles accepteren']")))
This is one way of accepting cookies on that website:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd
import time as t
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("window-size=1280,720")
webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
wait = WebDriverWait(browser, 20)
url = 'https://www.hornbach.nl/'
browser.get(url)
parent_el = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div[id="usercentrics-root"]')))
parent_el_shadow_root = parent_el.shadow_root
t.sleep(5)
accept_all_button = parent_el_shadow_root.find_element(By.CSS_SELECTOR, 'button[data-testid="uc-accept-all-button"]')
accept_all_button.click()
print('accepted cookies')
Bear in mind this way of locating the shadow root only works for Chrome/chromedriver, and you will need other methods if using Firefox or other browser/driver.
Selenium documentation can be found at https://www.selenium.dev/documentation/

Click on show more button; selenium scrape with python

I'm trying to scrape a website with show more button; and I'm not able to click on it.
The website is: https://www.wtatennis.com/rankings/singles
And my code is:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver import ActionChains
from tqdm import tqdm
import time
options = Options()
options.add_argument("--headless")
browser = webdriver.Chrome(ChromeDriverManager().install(),options=options)
browser.get('https://www.wtatennis.com/rankings/singles')
action = ActionChains(browser)
showmore = browser.find_elements_by_xpath(".//button[contains(#class, 'btn widget-footer__more-button rankings__show-more js-show-more-button')]")
action.move_to_element(showmore).perform()
showmore.click()
time.sleep(5)
Has anyone any idea? Thanks!
Don't use './/' in your locator when you are starting the search from root, as there is no current element your locator won't find any element. Also you can use any attribute to find elements uniquely. see below code:
browser = webdriver.Chrome(options=options)
browser.get('https://www.wtatennis.com/rankings/singles')
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,
'//*[#data-text="Accept Cookies"]'))).click()
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,
'//*[#data-text = "Show More"]'))).click()
use webdriver wait and data attributes
tu use wait import:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
To wait till all elements are loaded you have to make sure last element is not changing , if its changing keep scrolling .
browser.get('https://www.wtatennis.com/rankings/singles')
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,
'//*[#data-text="Accept Cookies"]'))).click()
value = "start"
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,
'//*[#data-text = "Show More"]'))).click()
while(browser.find_element_by_xpath("(//tr[#class='rankings__row'])[last()]").text != value):
elem = browser.find_element_by_xpath(
'(//*[contains(text(),"Loading")])[2]')
value = browser.find_element_by_xpath(
"(//tr[#class='rankings__row'])[last()]").text
browser.execute_script("arguments[0].scrollIntoView()", elem)
WebDriverWait(browser, 10).until(EC.presence_of_all_elements_located((By.XPATH,
"//tr[#class='rankings__row']")))
try:
WebDriverWait(browser, 10).until_not(EC.text_to_be_present_in_element((By.XPATH,
"(//tr[#class='rankings__row'])[last()]"), value))
except:
None

selenium - Not able to find input

Trying to create script which will subscribe to news automatically, but stuck with a problem, selenium not able to find email input and submit button. Everytime getting selenium.common.exceptions.NoSuchElementException:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = Options()
chrome_options.add_argument("--window-size=1920x1080")
path_to_chromedriver = 'chromedriver'
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=path_to_chromedriver)
driver.get('https://dataengweekly.com/')
driver.find_element_by_tag_name("body").send_keys(Keys.PAGE_DOWN)
email_input = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, 'input[type="email"]'))
)
email_input.send_keys("email#test.com")
driver.find_element_by_css_selector('button.subscribe-btn').click()
time.sleep(10)
Note - Your subscription textbox is in a different iframe, To work with that iframe you need to first switch to that iframe.
Try below code and let me know if you need more clarification -
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
import time
chrome_options = Options()
chrome_options.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(options=chrome_options)
wait = WebDriverWait(driver, 5)
action = ActionChains(driver)
driver.get('https://dataengweekly.com/')
iframe = driver.find_element_by_xpath('//iframe')
driver.switch_to.frame(iframe)
email_input = wait.until(EC.presence_of_element_located((By.XPATH, "//input[#type='email']")))
action.move_to_element(email_input).click().send_keys("email#test.com").perform()
driver.find_element_by_css_selector('button.subscribe-btn').click()
time.sleep(2)

How to locate and click "speed test" link on netflix using selenium in python?

I'm a complete beginner with selenium, sorry if the question is dumb (it is dumb):)
I need to find Speed test link on https://www.netflix.com/ and then click it.
i've tried searching by text and some other options. But nothing seems to work, I don't know why.
from selenium import webdriver
from selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://www.netflix.com/")
driver.implicitly_wait(10)
elem = driver.find_element_by_link_text("Speed test")
elem.click()
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Sign in"}
(Session info: chrome=75.0.3770.142)
The element with text as Speed Test is out ofthe Viewport so you need to induce WebDriverWait for the desired element to be clickable() and you can use the following Locator Strategy:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='footer-link']/span[text()='Speed Test']"))).click()
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
Use WebDriverWait and element_to_be_clickable with following xpath.
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.Chrome()
driver.get("https://www.netflix.com/")
elem = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//span[#data-uia='data-uia-footer-label'][contains(.,'Speed Test')]")))
elem.click()
Browser snapshot:
To added to this answer you need to use WebDriverWait and then click on the element Show more info
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.Chrome()
driver.get("https://www.netflix.com/")
elem = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//span[#data-uia='data-uia-footer-label'][contains(.,'Speed Test')]")))
elem.click()
WebDriverWait(driver,60).until(EC.element_to_be_clickable((By.XPATH,"//a[contains(.,'Show more info' )]"))).click()

how to Login gmail using Python Selenium

Hi all am trying to automate login the gmail account using python but am unable to do so , it just opens the gmail but nothing happens, what am i missing?
from selenium import webdriver
browser = webdriver.Chrome("C:/Users/INCT-KaviChand/Downloads/chromedriver_win32/chromedriver.exe")
browser.get('http://gmail.com')
emailElem = browser.find_element_by_id('Email')
emailElem.send_keys('kavi')
nextButton = browser.find_element_by_id('next')
nextButton.click()
passwordElem = browser.find_element_by_id('kavisam')
passwordElem.send_keys('MyPassword')
signinButton = browser.find_element_by_id('signIn')
signinButton.click()
also tried below one
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
browser = webdriver.Chrome("C:/Users/INCT-KaviChand/Downloads/chromedriver_win32/chromedriver.exe")
browser.get('http://gmail.com')
wait = WebDriverWait(browser, 10)
emailElem = browser.find_element_by_id('Email')
emailElem.send_keys("abc#gmail.com")
password_elem = wait.until(EC.presence_of_element_located((By.ID,'Passwd')))
password_elem.send_keys("xyz")
browser.find_element_by_name('signIn').click()
Try to find element in different way, code below is working for me:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path="C:/TestFiles/chromedriver.exe")
driver.get('http://gmail.com')
driver.find_element_by_xpath('//input[#type="email"]').send_keys('example', Keys.ENTER)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//input[#type="password"]')))
driver.find_element_by_xpath('//input[#type="password"]').send_keys('example', Keys.ENTER)

Resources