selenium doesn't want the website to get to the 3rd page - python

Whenever I want selenium to press enter for me, it doesn't want to, get to the next page.
Is something wrong with the code?
from selenium import webdriver
from selenium.webdriver.common import keys
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
from selenium.webdriver.common.keys import Keys
import time
PATH = "C:\Pro\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://insurify.com")
try:
search = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.ID, "zipcodeInput"))
)
search.send_keys('34997')
search.send_keys(Keys.RETURN)
element1 = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#typeahead-input > div > span > input:nth-child(2)"))
)
element1.send_keys("2016")
element1.send_keys(Keys.RETURN)
time.sleep(30)
element2 = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#typeahead-input > div > span.twitter-typeahead > input:nth-child(2)"))
)
element2.send_keys('BMW')
element2.send_keys(Keys.RETURN)
element3 = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#typeahead-input > div > span.twitter-typeahead > input:nth-child(2)"))
)
element3.send_keys('4-Series')
element3.send_keys(Keys.RETURN)
element4 = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#typeahead-input > div > span.twitter-typeahead > input:nth-child(2)"))
)
element4.send_keys('428i')
element4.send_keys(Keys.RETURN)
time.sleep(50)
except:
driver.quit
Also there's a picture for the last code execution of the code.

By running driver.implicitly_wait(30) right after the definition of driver, we can get rid of all the commands WebDriverWait(driver, 30).until(EC.presence_of_element_located((...))). Moreover, with a proper use of find_element() and click() we can replace the blocks of code such as
element1 = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#typeahead-input > div > span > input:nth-child(2)")))
element1.send_keys("2016")
element1.send_keys(Keys.RETURN)
with a one line command. The final code is
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
driver = webdriver.Chrome(service=Service(your_chromedriver_path))
driver.implicitly_wait(30)
driver.get("https://insurify.com")
driver.find_element(By.CSS_SELECTOR, '#zipcodeInput').send_keys('34997')
driver.find_element(By.XPATH, '//button[text()="View my quotes"]').click()
driver.find_element(By.XPATH, '//div[text() = "2016"]').click()
driver.find_element(By.XPATH, '//span[text()="BMW"]').click()
driver.find_element(By.XPATH, '//div[text()="4-Series"]').click()
driver.find_element(By.XPATH, '//div[text() = "428i"]').click()

Related

Trouble displaying links using python selenium

I'm trying to display a list of links that I parsed, it displays 1, when deriving a variable linkvideo2 I get the result in the form of the first link to the video. Maybe you can't use CSS_SELECTOR with a loop?)
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support import expected_conditions as EC
import time
name = 'hello world'
profile_path = r"C:/Users/Федорчик/Desktop"
options=Options()
options.set_preference('profile', profile_path)
driver = webdriver.Firefox(options=options)
#driver = webdriver.Firefox(r"C:/Users/Федорчик/Desktop")
driver.get('https://www.youtube.com')
id_serth = driver.find_element(By.NAME, "search_query")
id_serth.send_keys(name)
button_serth = driver.find_element(By.ID, "search-icon-legacy")
time.sleep(4)
button_serth.click()
time.sleep(4)
button_filtr = driver.find_element(By.CLASS_NAME ,"ytd-toggle-button-renderer")
button_filtr.click()
time.sleep(4)
button_filtrtode=driver.find_element(By.CLASS_NAME, "ytd-search-filter-renderer")
button_filtrtode.click()
time.sleep(4)
urltek = driver.current_url
linkvideo2 = driver.find_elements(By.CSS_SELECTOR, 'ytd-video-renderer.style-scope:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > h3:nth-child(1) > a:nth-child(2)')
links=[]
for i in linkvideo2:
links.append(i.get_attribute('href'))
print(len(links))
print (urltek)
Answer:
1
https://www.youtube.com/results?search_query=hello+world&sp=EgIIAQ%253D%253D
I will be very grateful for your help
better find by ID
linkvideo2 = driver.find_elements(By.ID, 'video-title')
links=[]
for i in linkvideo2:
links.append(i.get_attribute('href'))
print(len(links))
print(urltek)
print(links)
output of "links" will be something like this:
[None, None, None, None, None, 'https://www.youtube.com/shorts/uXMjlXBW_fg', 'https://www.youtube.com/watch?v=s0a7AWgo6CY', 'https://www.youtube.com/watch?v=SEgOG1fZwoM', 'https://www.youtube.com/watch?v=XXFdprzIxvc', 'https://www.youtube.com/watch?v=82YAs_viROM', 'https://www.youtube.com/watch?v=IgRObNaGhAg', 'https://www.youtube.com/watch?v=sFLPNXJU4KY', 'https://www.youtube.com/watch?v=64Wo6zQDgQ0', 'https://www.youtube.com/watch?v=SpvI9648RrU', 'https://www.youtube.com/watch?v=wUw-rh97fso', 'https://www.youtube.com/watch?v=C7XDmQsf1-A', 'https://www.youtube.com/watch?v=3md7eviZC_Y', 'https://www.youtube.com/watch?v=wklkXDhb6MQ', 'https://www.youtube.com/watch?v=82wxikm2CwI', 'https://www.youtube.com/watch?v=D1DVUYcE43A', 'https://www.youtube.com/watch?v=MvFgCJqpxWI', 'https://www.youtube.com/shorts/8cfy5DjIS10', 'https://www.youtube.com/watch?v=5RV3unTmyTA', 'https://www.youtube.com/watch?v=Sjgee-HCCxs', 'https://www.youtube.com/watch?v=vUGQsCQNUgs']

How do I click and press keys using Selenium?

I'm trying to find a Google Place ID of Statue of Liberty National Monument from a Google's developer website: https://developers.google.com/maps/documentation/javascript/examples/places-placeid-finder#maps_places_placeid_finder-css
I want to click on the search bar where it says "Enter a location", type "Statue of Liberty National Monument", press down arrow key, and press enter key to get this result.
However, my code doesn't even click the search bar. Please help me.
import selenium
import pandas as pd
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
Google_IDS = ['https://developers.google.com/maps/documentation/javascript/examples/places-placeid-finder#maps_places_placeid_finder-css']
for Google_ID in Google_IDS:
driver.get(Google_ID)
driver.implicitly_wait(10)
Google_ID = driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div[4]/input')
Google_ID.click()
Google_ID.send_keys('Statue of Liberty National Monument')
Google_ID.send_keys('keys.DOWN')
Google_ID.send_keys('keys.ENTER')
Updated Code:
import selenium
import pandas as pd
import time
from selenium import webdriver
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
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
wait = WebDriverWait(driver, 10)
Google_IDS = ['https://developers.google.com/maps/documentation/javascript/examples/places-placeid-finder#maps_places_placeid_finder-css']
for Google_ID in Google_IDS:
driver.get(Google_ID)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"div.devsite-article-body.clearfix > div > devsite-iframe > iframe")))
Google_ID = driver.find_element(By.CSS_SELECTOR, '#pac-input')
Google_ID.send_keys('Statue of Liberty National Monument')
dropdown = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.pac-container.pac-logo > div')))
dropdown.click()
ID_Info = driver.find_element(By.ID, "place-id")
print("Google ID:", ID_Info.text)
Try Use following code
PATH = "C:\Program Files(x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
Google_IDS = ['https://developers.google.com/maps/documentation/javascript/examples/places-placeid-finder#maps_places_placeid_finder-css']
for Google_ID in Google_IDS:
driver.get(Google_ID)
driver.implicitly_wait(10)
Google_ID = driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div[4]/input')
Google_ID.click()
Google_ID.send_keys('Statue of Liberty National Monument')
Google_ID.send_keys(Keys.DOWN)
Google_ID.send_keys(Keys.ENTER)
For more :https://pythonbasics.org/selenium-keyboard/
There is another solution with using Exmplity wait and also you don't need to press down key may just Enter key
PATH = "C:\Program Files(x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
Google_IDS = ['https://developers.google.com/maps/documentation/javascript/examples/places-placeid-finder#maps_places_placeid_finder-css']
element = WebDriverWait(driver, 10).until(
for Google_ID in Google_IDS:
driver.get(Google_ID)
EC.presence_of_element_located((By.CSS_SELECTOR,"div.devsite-article-body.clearfix > div > devsite-iframe > iframe"))
Google_ID = driver.find_element(By.CSS_SELECTOR,"div.devsite-article-body.clearfix > div > devsite-iframe > iframe")
Google_ID.click()
Google_ID.send_keys('Statue of Liberty National Monument')
Google_ID.send_keys(Keys.ENTER)
wait = WebDriverWait(driver, 10)
Google_IDS = ['https://developers.google.com/maps/documentation/javascript/examples/places-placeid-finder#maps_places_placeid_finder-css']
for Google_ID in Google_IDS:
driver.get(Google_ID)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"div.devsite-article-body.clearfix > div > devsite-iframe > iframe")))
Google_ID = driver.find_element(By.CSS_SELECTOR, '#pac-input')
Google_ID.send_keys('Statue of Liberty National Monument')
dropdown = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.pac-container.pac-logo > div')))
dropdown.click()
Your element is in an iframe and the way to send non strings such as keys are as follows. Also don't use a generic xpath as those are brittle and generally will break instead use a more inclusive selector.
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Unable to click on suggestion provided by textbox (Selenium)

When I send text manually, suggestion area provided by first textbox , it works fine. But when I send the text using selenium library it cannot able to select the option from suggestion area, although that option is present inside the textbox. Is there any one who can help me out of that.
import time
from selenium.webdriver.support.ui import Select
path=r"C:\Users\AbdulRehman\Downloads\chromedriver_win32\chromedriver.exe"
# driver = webdriver.Chrome(path)
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
driver.get("https://www4.sii.cl/mapasui/internet/#/contenido/index.html")
try:
element = WebDriverWait(driver, 1000).until(
EC.presence_of_element_located((By.XPATH, '//*[#id="ng-app"]/body/div[5]/div/div/div[3]/div/button'))
)
element.click()
print("prints its working fine now ..")
element = WebDriverWait(driver, 1000).until(
EC.presence_of_element_located((By.XPATH,'//*[#id="titulo"]/div[8]/i'))
)
element.click()
# element = WebDriverWait(driver, 1000).until(
# EC.presence_of_element_located((By.XPATH,'//*[#id="rolsearch"]/div[2]/div[1]/input'))
# )
# element.send_keys("PEDRO AGUIRRE CERD"+Keys.ENTER)
# search = WebDriverWait(driver, 60).until(
# EC.visibility_of_element_located((By.XPATH, '//*[#id="rolsearch"]/div[2]/div[1]/input'))
# )
# search.send_keys("EL MONTE" + Keys.ENTER)
# time.sleep(3)
search = WebDriverWait(driver, 60).until(
EC.visibility_of_element_located((By.XPATH, '//*[#id="rolsearch"]/div[2]/div[1]/input'))
)
ActionChains(driver).click(on_element=search).send_keys("EL MONTE").send_keys(Keys.ENTER).perform()
suggestion = WebDriverWait(driver, 60).until(
EC.visibility_of_element_located((By.XPATH, '//strong[text()="EL MONTE"]'))
)
suggestion.click()
# auto_complete = driver.find_elements_by_xpath('//*[#id="rolsearch"]/div[2]/div[1]/input')
# auto_complete[0].click()
# auto_complete.send_keys(Keys.RETURN)
# element.send_keys("somehting in text")
# search = driver.find_element_by_xpath().click()
# search.send_keys(Keys.RETURN)
search_1 = driver.find_element_by_xpath('//*[#id="rolsearch"]/div[2]/div[2]/input')
search_1.send_keys("PEDRO AGUIRRE CERDA")
search_1.send_keys(Keys.RETURN)
search_2 = driver.find_element_by_xpath('//*[#id="rolsearch"]/div[2]/div[3]/input')
search_2.send_keys("somehting in text")
search_2.send_keys(Keys.RETURN)
print("Its also working now ......")
time.sleep(3)
except Exception as e:
print(e)
driver.quit()
The desired element is a Angular element, so to send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:
Using XPATH:
driver.get('https://www4.sii.cl/mapasui/internet/#/contenido/index.html')
WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Aceptar']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//i[#data-ng-click='mostrarBusquedaRol()']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(#data-ng-include, '/mapasui/common/_content/busqueda-rol.html')]//div[#id='rolsearch']//label[contains(., 'Comuna')]//following::input[1]"))).send_keys("PEDRO AGUIRRE CERD" + Keys.ENTER)
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
Browser Snapshot:

Attempt to print all records in a table only get's the first 16 rows

When I run the below Python program the statement
print([
my_elem.text for my_elem in
WebDriverWait(driver, 20)
.until(EC.visibility_of_all_elements_located(
(By.XPATH,
"/html/body/div[1]/ui-view/div/div[1]"
"/div/div/div/div/exploration-container/exploration-container-modern"
"/div/div/exploration-host/div/div/exploration/div/explore-canvas-modern"
"/div/div[2]/div/div[2]/div[2]/visual-container-repeat/visual-container-modern[9]/transform"
"/div/div[3]/detail-visual-modern/div/visual-modern/div/div/div[2]/div[1]"
)
))
])
should print all records in the table but it only prints the first 16 rows plus 4 rows from the second column. How can I get all rows printed?
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("https://app.powerbi.com/view?r=eyJrIjoiZGYxNjYzNmUtOTlmZS00ODAxLWE1YTEtMjA0NjZhMzlmN2JmIiwidCI6IjljOWEzMGRlLWQ4ZDctNGFhNC05NjAwLTRiZTc2MjVmZjZjNSIsImMiOjh9")
for i in range(1, 4):
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='navigation-wrapper navigation-wrapper-big']//i[#title='Next Page']"))).click()
action = ActionChains(driver)
action.move_to_element(driver.find_element_by_xpath("/html/body/div[1]/ui-view/div/div[1]/div/div/div/div/exploration-container/exploration-container-modern/div/div/exploration-host/div/div/exploration/div/explore-canvas-modern/div/div[2]/div/div[2]/div[2]/visual-container-repeat/visual-container-modern[9]/transform")).perform()
action.context_click().perform()
element = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[#title='Show as a table']")))
action.move_to_element(element).click().perform()
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "/html/body/div[1]/ui-view/div/div[1]/div/div/div/div/exploration-container/exploration-container-modern/div/div/exploration-host/div/div/exploration/div/explore-canvas-modern/div/div[2]/div/div[2]/div[2]/visual-container-repeat/visual-container-modern[9]/transform/div/div[3]/detail-visual-modern/div/visual-modern/div/div/div[2]/div[1]")))])
driver.quit()
Additional info
I managed to get this working although it feels a little hacky. Here's my latest code. You'll see there's a second ActionChain:
driver = webdriver.Firefox()
driver.get("https://app.powerbi.com/view?r=eyJrIjoiZGYxNjYzNmUtOTlmZS00ODAxLWE1YTEtMjA0NjZhMzlmN2JmIiwidCI6IjljOWEzMGRlLWQ4ZDctNGFhNC05NjAwLTRiZTc2MjVmZjZjNSIsImMiOjh9")
for i in range(1, 4):
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//i[#title='Next Page']"))).click()
action = ActionChains(driver)
action.move_to_element(driver.find_element_by_xpath("/html/body/div[1]/ui-view/div/div[1]/div/div/div/div/exploration-container/exploration-container-modern/div/div/exploration-host/div/div/exploration/div/explore-canvas-modern/div/div[2]/div/div[2]/div[2]/visual-container-repeat/visual-container-modern[9]/transform")).perform()
action.context_click().perform()
element = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[#title='Show as a table']")))
action.move_to_element(element).click().perform()
action2 = ActionChains(driver)
scroll_element = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/ui-view/div/div[1]/div/div/div/div/exploration-container/exploration-container-modern/div/div/exploration-host/div/div/exploration/div/explore-canvas-modern/div/div[2]/div/div[2]/div[2]/visual-container-repeat/visual-container-modern[9]/transform/div/div[3]/detail-visual-modern/div/visual-modern/div")))
action2.move_to_element(scroll_element).click()
action2.send_keys(Keys.UP)
action2.send_keys(Keys.UP)
action2.send_keys(Keys.UP).perform()
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "/html/body/div[1]/ui-view/div/div[1]/div/div/div/div/exploration-container/exploration-container-modern/div/div/exploration-host/div/div/exploration/div/explore-canvas-modern/div/div[2]/div/div[2]/div[2]/visual-container-repeat/visual-container-modern[9]/transform/div/div[3]/detail-visual-modern/div/visual-modern/div/div/div[2]/div[1]")))])
Can this be done with just one ActionChain and if so how?

Extracting Reviews of one particular app from Google Play Store using Selenium Python

from time import sleep
from webbrowser import Chrome
import selenium
from bs4 import BeautifulSoup as bsoup
import pandas as pd
from selenium import webdriver
class FindByXpathCss():
def test(self):
baseUrl = "https://play.google.com/store/apps/details?
id=com.delta.mobile.android&hl=en_US&showAllReviews=true"
driver = webdriver.Chrome("F:\\Chrome-webdriver\\chromedriver")
driver.maximize_window()
driver.get(baseUrl)
Here I need to click on one button (Full review) to view the full review text.
fullReviewbtn = driver.find_element_by_css_selector('#fcxH9b > div.WpDbMd > c-wiz > div >
div.ZfcPIb > div > div.JNury.Ekdcne > div > div > div.W4P4ne > div:nth-child(2) > div >
div:nth-child(2) > div > div.d15Mdf.bAhLNe > div.UD7Dzf > span:nth-child(1) > div >
button').click()
sleep(1)
Here we are reading that full review text using an xpath, but I wish to read all other reviews of
the app, around 1200 reviews for this app alone. I wish to know how can i iterate it using for
loop here.
elementByXpath = driver.find_element_by_xpath('//*
[#id="fcxH9b"]/div[4]/c-wiz/div/div[2]/div/div[1]/div/div/div[1]/div[2]/div/div[2]/div/div[2]/div[2]').text
if elementByXpath is not None:
print("We found an element using Xpath")
#Review = elementByXpath.get_attribute("Review")
print(elementByXpath)
driver.close()
ff = FindByXpathCss()
ff.test()
import time
from selenium import webdriver
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.common.exceptions import TimeoutException
class FindByXpathCss():
driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
driver.maximize_window()
baseUrl = "https://play.google.com/store/apps/details?id=com.delta.mobile.android&hl=en_US&showAllReviews=true"
driver.get(baseUrl)
scrolls = 15
while True:
scrolls -= 1
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
time.sleep(3)
if scrolls < 0:
break
elemtn = WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, "//span[contains(#class,'RveJvd snByac')]")))
elemtn.click()
scrolls = 5
while True:
scrolls -= 1
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
time.sleep(3)
if scrolls < 0:
break
elemtn = WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, "//span[contains(#class,'RveJvd snByac')]")))
elemtn.click()
reviewText = WebDriverWait(driver, 30).until(
EC.presence_of_all_elements_located((By.XPATH, "//*[#class='UD7Dzf']")))
# reviewText = driver.find_elements_by_xpath("//*[#class='UD7Dzf']")
for textreview in reviewText:
print textreview.text

Categories