Selenium clicks next button instead of back button - python

Trying to scrape this webpage for prices, and I need the prices to be in US dollars, so it is currency I understand. However, when I initially load the URL, it gives the prices in multiple seemingly random currencies. I found that I could change this by clicking the next button, and then the back button, but when I tried to automate this, it did not work. Instead, running this code clicks the next button twice, rather than clicking it once, waiting for five seconds, and then clicking the back button. Here is the code that I am currently using that can replicate this problem.
from selenium import webdriver
driver = webdriver.Chrome(r'C:\Users\Hank\Desktop\chromedriver_win32\chromedriver.exe')
driver.get('https://steamcommunity.com/market/listings/440/Unusual%20Old%20Guadalajara')
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support.expected_conditions import presence_of_element_located
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import StaleElementReferenceException
import time
time.sleep(5)
action = ActionChains(driver)
next_button=wait(driver, 10).until(EC.element_to_be_clickable((By.ID,'searchResults_btn_next')))
action.move_to_element(next_button).click().perform()
time.sleep(5)
back_button=wait(driver, 10).until(EC.element_to_be_clickable((By.ID,'searchResults_btn_prev')))
action.move_to_element(back_button).click().perform()
Thanks, your time and help is greatly appreciated. Please direct me to a relevant question if this one has already been answered somewhere else.

You don't need ActionChains class, it's works by .click() method.
Try following code:
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(r'C:\Users\Hank\Desktop\chromedriver_win32\chromedriver.exe')
driver.get('https://steamcommunity.com/market/listings/440/Unusual%20Old%20Guadalajara')
wait = WebDriverWait(driver, 20)
next_button = wait.until(EC.element_to_be_clickable((By.ID,'searchResults_btn_next')))
next_button.click()
time.sleep(5)
back_button = wait.until(EC.element_to_be_clickable((By.ID,'searchResults_btn_prev')))
back_button.click()
But note, time.sleep(5) is bad way, you can use other way, ex : wait until the second page element appear.
Or instead of time.sleep(...) in this case, you can use this code:
wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR,'.pagebtn.disabled')))
The above is the disable previous button since you landing in the first pagination, and will gone when you arrieve on second pagination. Use .invisibility_of_element_located, it will more efficient.

Related

Using Selenium to accept a splash page

When I load the my site as shown below, a splash screen appears to confirm I am 21 years of age. I am trying to load the element to click yes, but I am unable to bypass the age verification screen.
My approach was to load the page. Add a sleep time, find the element and click Yes. However it wont work.
driver.get('https://seelbachs.com/products/sagamore-spirit-cask-strength-rye-whiskey')
time.sleep(5)
element= driver.find_element_by_xpath('//*[#id="enter"]')
element.click()
Error received.
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="enter"]"}
So either my find.element is off or my time.sleep is not helping with the splash page.
It seems the problem is that the pop-up is in an iframe.
This code appears to do the trick
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 NoSuchElementException
from selenium.webdriver import ActionChains
import time
driver = webdriver.Chrome('/usr/local/bin/chromedriver')
driver.get('https://seelbachs.com/products/sagamore-spirit-cask-strength-rye-whiskey')
# xpath of iframe
frame_xpath = '/html/body/div[5]/div/div/div/div/iframe'
wait = WebDriverWait(driver, 10)
# wait until iframe appears and select iframe
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, frame_xpath)))
# select button
xpath = '//*[#id="enter"]'
time.sleep(2)
element= driver.find_element_by_xpath(xpath)
ActionChains(driver).move_to_element(element).click(element).perform()
referencing This answer on Stack Overflow
You need to switch to the frame, check the below code
driver.get('https://seelbachs.com/products/sagamore-spirit-cask-strength-rye-whiskey')
sleep(5)
driver.switch_to.frame(driver.find_element_by_xpath("//iframe[contains(#id,'fancyboxAge-frame')]"))
OR
#Using the explicitWait
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(#id,'fancyboxAge-frame')]")))
element= driver.find_element_by_xpath('//*[#id="enter"]')
element.click()
import
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

send.keys(Keys.ENTER) is not working in Python

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
browser=webdriver.Chrome('C:/Users/Dell/Downloads/chromedriver')
browser.get('https://www.screener.in/')
sbox = browser.switch_to.active_element
sbox.send_keys('Infosys Ltd')
sbox.send_keys(Keys.RETURN)
The enter key is not working. I have tried using .submit() too but still isn't working. Please let me know if there is any other way to get it.
Try using Keys.ENTER instead of Keys.RETURN
url = "https://www.foodpanda.pk/restaurants/new?lat=24.9414896&lng=67.1676002&vertical=restaurants"
browser = webdriver.Chrome()
browser.get('https://www.screener.in/')
sbox = browser.switch_to.active_element
sbox.send_keys('Infosys Ltd')
WebDriverWait(browser, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, '[class="dropdown-content visible"]')))
sbox.send_keys(Keys.ENTER)
wait for the dropdown to be visible before sending the enter
imports required:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
switch_to may be unstable depending on browsers. Also, make sure sbox is visible before you interact with it. Try:
sbox['value'].send_keys('Infosys Ltd')
sbox['value'].send_keys(Keys.RETURN)

How to login popup windows?

i'm trying to login this site with selenium -python
but i can't .i read another question but i didn't get it.
how can i do it?
this my error:
enter image description here
this is my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome()
driver.get("https://en.gamefa.com/")
driver.find_element_by_name("loginform").submit()
driver.find_element_by_class_name("ModalBoxBody")#.submit()
time.sleep(10)
elem1 = driver.find_element_by_name("log")
elem1.send_keys("mehrdad78")
elem2 = driver.find_element_by_name("pwd")
elem2.send_keys("mehrdad78").submit()
First, Your screenshot is not giving us enough data about the error. Post the error stack trace which directs to your code.
Second, all the items have id. It would be better to use id to find elements.
Third, Click does work. Submit doesn't work on buttons. using submit on an element outside a form should throw an exception.
Fourth, Use explicit wait instead of sleep.
Try this
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
driver.get("https://en.gamefa.com/")
driver.find_element_by_id("login-form").click()
elem1 = WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.ID, "user_login3")).send_keys("mehrdad78")
elem2 = driver.find_element_by_id("user_pass3").send_keys("mehrdad78")
elem3 = driver.find_element_by_id("wp-submit3").submit()

Selenium Python using ActionChains

The code I have opens a website and using actionChains, it right clicks on the desktop which brings up a menu. I now need to do 3 more things with actionChains. I need to hover over the item that says Save Page WE and then click an item on the sub-menu that pops up and then hit the enter button. Can anyone show me how to do this? Thanks
from selenium import webdriver
from selenium.webdriver import ActionChains
fp = webdriver.FirefoxProfile('/Users/Jake/AppData/Roaming/Mozilla/Firefox/Profiles/emjx467y.default-1524932841911')
driver = webdriv[enter link description here][1]er.Firefox(fp)
driver.get('http://www.tradingview.com/screener')
element = driver.find_element_by_link_text('Screener')
actionChains = ActionChains(driver)
actionChains.context_click(element).perform()
By using this line : actionChains.context_click(element).perform() , you are trying to right click on Screener menu. But the ideal behavior should be to be hover on it and select one options out of 3.
I'm selecting Forex Screener, you can select any one as per your requirement.
For hover over you can try this code :
actionChains.move_to_element(element).perform()
Full code would be like this :
driver.get("http://www.tradingview.com/screener")
wait = WebDriverWait(driver,40)
driver.find_element_by_css_selector("span[class*='tv-header__dropdown-wrap--noarrow'] span[class$='lang-icon--current-lang']").click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='English (UK)']/parent::a"))).click()
element = driver.find_element_by_link_text('Screener')
actionChains = ActionChains(driver)
actionChains.move_to_element(element).perform()
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Forex Screener"))).click()
Make sure to import these :
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
from selenium.webdriver.common.action_chains import ActionChains

Tab on Website is "Not Clickable" Using Selenium w/ Python

I'm trying to use Selenium to click the tab for quarterly financials on this page:
http://www.msn.com/en-us/money/stockdetails/financials/fi-126.1.AAPL.NAS
When I run my code, it works some of the time, and sometime it tells me:
"Element is not clickable at point (897.7999877929688, 20.100006103515625). Other element would receive the click:
<span class="mectrlname mectrlsignin"></span>"
Here is the code I am running...
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.common.exceptions import *
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox()
driver.get('http://www.msn.com/en-us/money/stockdetails/financials/fi-126.1.AAPL.NAS')
wait = WebDriverWait(driver, 3)
qtrtab = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[#id="financials-period-list"]/li[2]')))
qtrtab.click()
Does anyone know why sometimes I get the error message and other times it works just fine? Should I be doing this differently? Thanks!
There is a "frozen" header that covers the element you want to click when the cursor is moved to it. Just maximize the browser window to avoid this problem:
driver = webdriver.Firefox()
driver.maximize_window()

Categories

Resources