ActionChains doesn't work, unless I manually refresh the selenium browser - selenium

I'm using selenium, and in the middle of the automation, this code only works if I manually refresh the browser.
If I do driver.get("URL"), or driver.refresh(), it doesn't help.
from selenium.webdriver.common.action_chains import ActionChains
elems = driver.find_elements_by_tag_name('div')
elem = elems[3]
action = ActionChains(driver)
action.click(elem)
action.send_keys("text")
action.perform()
I already can't use elem.send_keys(), it doesn't work anyway or anyhow. Though elem.click() does, that doesn't help if elem.send_keys() doesn't.

I think you can use explicit wait in this case like this :
from selenium.webdriver.common.action_chains import ActionChains
wait = WebDriverWait(driver, 50)
elems = driver.find_elements_by_tag_name('div')
elem = elems[3]
action = ActionChains(driver).move_to_element(elem).perform()
wait.until(EC.visibility_of((elem))).click()
time.sleep(2)
wait.until(EC.visibility_of((elem))).send_keys("text")
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Related

Selenium will not click class id

I am trying to have selenium to do the following:
Open a website
Click on the search box
Type "Seattle" in the search box
Select the first result from the suggested results
My code fails at Step 2.
The class id for the search box is "class = input_search ng-pristine ng-valid ng-empty ng-touched"
Here's my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
url = 'https://wego.here.com/'
driver.get(url)
driver.find_element_by_css_selector('.input_search.ng-pristine.ng-valid.ng-empty.ng-touched').click()
driver.find_element_by_css_selector('.input_search.ng-pristine.ng-valid.ng-empty.ng-touched').send_keys('Seattle')
driver.find_element_by_css_selector('.input_search.ng-pristine.ng-valid.ng-empty.ng-touched').send_keys(Keys.ENTER)
Any suggestions would be greatly appreciated!
ADDITIONAL QUESTION
Thanks to #Prophet I was able to get the first auto-click and auto-fill done, but when I try to do the same task with a different search box, it does not like it. Please refer to the following code that I added to the existing code:
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.btn"))).send_keys(Keys.ENTER)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.itinerary_item_input_0"))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.itinerary_item_input_0"))).send_keys('Chicago')
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.itinerary_item_input_0"))).send_keys(Keys.ENTER)
button.btn did work, but not the input.itinerary_item_input_0. Here is the source screenshot:
You are using a wrong locator.
ng-empty and ng-touched may not be there all the times.
So instead of
driver.find_element_by_css_selector('.input_search.ng-pristine.ng-valid.ng-empty.ng-touched').click()
Try using this:
driver.find_element_by_css_selector('input.input_search').click()
input.input_search is an unique, stable locator for that element.
Also, you have to add a delay, preferably to use the expected conditions, as following:
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()
wait = WebDriverWait(driver, 20)
url = 'https://wego.here.com/'
driver.get(url)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).click()
wait=WebDriverWait(driver, 60)
driver.get('https://wego.here.com/')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input.input_search"))).send_keys("Seattle")
wait.until(EC.element_to_be_clickable((By.XPATH,"//div[#class='dropdown_list']/div[1]"))).click()
This will select the first option after sending a string to input tag.
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

How to scroll to the end Selenium Python

I'm trying to scroll to the end in this page url
When got into the page, I click the button 'Show all 77 products' I got into a popup that shows partially the elements into the popup. Actually this is my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
def getpage(driver):
driver.get('https://www.binance.com/it/pos')
sleep(3)
driver.find_element_by_xpath('//div[#id="savings-lending-pos-expend"]').click()
sleep(2)
elem = driver.find_element_by_xpath('//div[#class="css-n1ers"]')
elem.send_keys(Keys.END)
driver = webdriver.Firefox()
getpage(driver)
I have tried almost everything to work, apart from the solution in the code above, I tried with nu success the current solutions:
driver.execute_script("window.scrollTo(0, Y)")
and
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
and in this solutions I didn't understand which label to use
label.sendKeys(Keys.PAGE_DOWN);
I tried almost all solutions but none worked. I hope you can help me. Thank you.
Try like below and confirm:
You can try to find each row and apply scrollIntoView for the same.
# Imports required for Explicit wait:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
def getpage(driver):
driver.get('https://www.binance.com/it/pos')
wait = WebDriverWait(driver,30)
wait.until(EC.element_to_be_clickable((By.ID,"savings-lending-pos-expend"))).click() // show more option
i = 0
try:
while True:
options = driver.find_elements_by_xpath("//div[#id='modal-wrapper']/div") // Try to find rows.
driver.execute_script("arguments[0].scrollIntoView(true);",options[i])
time.sleep(1)
i+=1
except IndexError as e:
print("Exception: {}".format(e.args[-1]))
print(i)
getpage(driver)
You can use ActionChains
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
actions.send_keys(Keys.PAGE_DOWN).perform()
That will make the page scroll down similar to pressing the Page Down key.
This solution worked:
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
from time import sleep
def getpage(driver):
driver.get('https://www.binance.com/it/pos')
wait = WebDriverWait(driver,30)
wait.until(EC.element_to_be_clickable((By.ID,"savings-lending-pos-expend"))).click()
i = 0
sleep(5)
pop_up_window = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='modal-scroller']")))
while True:
driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', pop_up_window)
sleep(1)
driver = webdriver.Firefox()
getpage(driver)

Selenium return empty list from morningstar

I'm trying to extract info from morningstar but driver return back a list empty.
here's the code:
from selenium import webdriver
import time
import datetime
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(<<...>>)
driver.implicitly_wait(1)
driver.maximize_window()
driver.get('https://www.morningstar.it/it/etf/snapshot/snapshot.aspx?id=0P0001J2VK&tab=3&InvestmentType=FE')
time.sleep(5)
driver.find_element_by_id("individualinvestor").click()
time.sleep(1)
driver.find_element_by_id("_evidon-accept-button").click()
time.sleep(1)
usStock=driver.find_elements_by_xpath('//*[#class="sal-asset-allocation__assetTableRow sal-asset-allocation__assetTableRow--usStock"]/td[2]')
thx
That is because it is in iframe, so you need to switch it first like this :
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "portfolio")))
usStock = driver.find_elements_by_xpath('//*[#class="sal-asset-allocation__assetTableRow sal-asset-allocation__assetTableRow--usStock"]/td[2]')
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
also, I would scrape it like this (since time.sleep() is not recommended at all) so you can use Explicit wait like this :
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://www.morningstar.it/it/etf/snapshot/snapshot.aspx?id=0P0001J2VK&tab=3&InvestmentType=FE")
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.ID, "individualinvestor"))).click()
wait.until(EC.element_to_be_clickable((By.ID, "_evidon-accept-button"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "portfolio")))
usStock = driver.find_elements_by_css_selector("tr[class*='al-asset-allocation'] td")
print(len(usStock))
for stock in usStock:
print(stock.text)

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,

How to use Selenium webdriver select date on below URL

http://www.hkexnews.hk/sdw/search/mutualmarket.aspx?t=sh
I find the element by ID 'txtShareholdingDate' and then I tried use "send_keys('201 8/10/24') not work
Any ideas. Thanks
readonly attribute means that you cannot set the value of text field with send_keys method.
If you don't need your script to simulate user-like behavior, you can implement JavaScriptExecutor as below
input_field = driver.find_element_by_id('txtShareholdingDate')
driver.execute_script('arguments[0].value="2018/10/24";', input_field)
driver.find_element_by_xpath('//input[#type="submit"]').click()
If you need to select date as user do:
import time
input_field = driver.find_element_by_id('txtShareholdingDate')
input_field.click()
time.sleep(1)
driver.find_element_by_xpath('//b[#class="year"]//li[.="2018"]').click()
time.sleep(1)
driver.find_element_by_xpath('//b[#class="month"]//li[.="10"]').click()
time.sleep(1)
driver.find_element_by_xpath('//b[#class="day"]//li[.="24"]').click()
time.sleep(1)
driver.find_element_by_xpath('//input[#type="submit"]').click()
P.S. You also might need to use ExplicitWait instead of time.sleep(1):
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
input_field = driver.find_element_by_id('txtShareholdingDate')
input_field.click()
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//b[#class="year"]//li[.="2017"]'))).click()
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//b[#class="month"]//li[.="11"]'))).click()
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//b[#class="day"]//li[.="22"]'))).click()
driver.find_element_by_xpath('//input[#type="submit"]').click()