slider locating and automating with Selenium python - python

I'm trying to automate a log in on a website. I have the following code:
def __init__(self):
self.driver = webdriver.Chrome(executable_path = '/usr/bin/chromedriver')
def parse(self, response):
self.driver.get(response.url)
self.driver.switch_to.frame(self.driver.find_element_by_id('J_loginIframe'))
self.driver.find_element_by_name('fm-login-id').send_keys('iamgooglepenn')
self.driver.find_element_by_id('fm-login-password').send_keys('mypassword')
self.driver.find_element_by_class_name('fm-button fm-submit password-login').click()
Right now, this code successfully put in the log in information and click the login button; However, the web asks my spider to slide a bar to the right before logging me in.
The HTML of the slidebar is as follows:
<span id="nc_1_n1z" class="nc_iconfont btn_slide" data-spm-anchor-id="0.0.0.i3.6a38teDwteDwKs" style="left:-2px";> ▫ </span>
Is there a way to automate this with python?

I think this s because you've visited the website multiple times on that device. So if you login from a new device, that slider will most probably turn up. So to access the slider, delete your caches and cookies and try again (or use Incognito mode). Or when you see the slider during the test, right click and inspect.
So the next time when you run it, make sure to click the slider using an xpath
Hope this works!!

Related

Selenium WebDriver Python 3 Refresh Page Until Text Found And Afterwards Click To Buy

I am a total noob that tries to get together a code that will do below things.
1.) Log in with my credentials to the page https://www.alza.sk/EN/
2.) Refreshes the page in a specific interval, for example "60 seconds"
3.) Checks for the “In stock” text on any product displayed or a specific one product that will be opened in the one browser tab"
4.) Click on the “Add to cart” button beside the specific product that will be opened in the mentioned one browset tab and continue to the shopping cart by clickiny on “Proceed to Checkout” button, afterwards click on “Continue” button, if popup window displays with two buttons “Do not add anything” and “Add the selected items to your cart“ then click on the first button “Do not add anything”. In the next page choose the “Bratislava - main shop” checkbox and after that choose “Confirm your selection” button in the popup window. Afterwards click on “All payments option” section so all options are displayed and choose the “Cash / Card (when collected) checkbox. After that click on “Continue” button. And this is the step when i need to login by entering my credentials. So this i”ll update later today. But i suppose it would be best to log in in the Step 1.) as i described.
This is all i could so far get together, but i am unable to add all i described, so it will work.
from selenium import webdriver
import time
import urllib
driver = webdriver.Chrome()
driver.get("https://www.alza.sk/EN/")
while(True):
driver.refresh()
time.sleep("60")
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("input url here")
expected_text="Available"
actual_text=driver.find_element(By.XPATH, '//button[text()="Available"]').text
while actual_text!=True:
driver.refresh()
time.sleep(input time interval here)
if expected_text==actual_text:
actual_text=True
driver.find_element(By.XPATH, '//button[text()="Available"]').click()
#add other steps
This is just a suggestion on how to proceed with the script. If you can update the question with the URL and proper code, it will be easier to give a proper solution.

How to perform data fetch on button click on a built in html page using selenium

I am new to Selenium and I am trying to mimic user actions on a site to fetch data from a built in html page on button click. I am able to populate all the field details, but button click is not working, it looks like js code not running.
I tried many options like adding wait time, Action chain etc but it didnt work, i am providing site and code i have written.
driver = webdriver.Chrome()
driver.get("https://www1.nseindia.com/products/content/derivatives/equities/historical_fo.htm")
driver.implicitly_wait(10)
assigned values to all the other fields
driver.find_element_by_id('rdDateToDate').click()
Dfrom = driver.find_element_by_id('fromDate')
Dfrom.send_keys("02-Oct-2020")
Dto = driver.find_element_by_id('toDate')
Dto.send_keys("08-Oct-2020")
innerHTML = driver.execute_script("document.ready")
sleep(5)
getdata_btn = driver.find_element_by_id('getButton')
ActionChains(driver).move_to_element(getdata_btn).click().click().perform()
I recommend using a full xpath.
chrome.get("https://www1.nseindia.com/products/content/derivatives/equities/historical_fo.htm")
time.sleep(2)
print("click")
fullxpath = "/html/body/div[2]/div[3]/div[2]/div[1]/div[3]/div/div[1]/div/form/div[19]/p[2]/input"
chrome.find_element_by_xpath(fullxpath).click()
I have tried the button clicking and it worked with XPath ... I though its because of someone used the ID twice on a website, but I can not find it ... so i have no idea whats going wrong there ...
Good luck :)

How to avoid login each time with Selenium python

I have the following code to login in a website.
from selenium import webdriver
driver = webdriver.Chrome("C:\webdrivers\chromedriver.exe")
driver.get ("https://examplesite.com")
driver.find_element_by_id("username").send_keys("MyUsername")
driver.find_element_by_id("password").send_keys("MyPassword")
I do some clicks in that homepage and then a second page https://secondpage.com/some/text is opened in a different tab. I need to make some automation testing
in this second page but if I try to work directly in second page changing in my above code from this
driver.get ("https://examplesite.com")
to this
driver.get ("https://secondpage.com/some/text")
I'm being redirected to first page https://examplesite.com to login again.
I´ve tried to pass the credentials directly in get command like this:
driver.get ("https://MyUsarname:MyPassword#secondpage.com/some/text")
but the same happens and I'm redirected to the login page.
Is there a way to run the script directly in second page without need to login each time I test something?
Maybe mantain in memory Selenium that I´m already logged in?
Thanks for any help

How click on dynamic buttons link "#" from selenium and splinter?

I am trying to scrap something from website (example facebook(not using graph api just doing for learning), so I successfully login and land on front page, where I want to scrap some data, but the problem is when I land on front page, then facebook shows a layer and a box which says "turn on notification", now without click on any button between "Not Now" or "turn on" I can't do anything with splinter, and when I tried to click splinter doesn't do anything because the link of those button are "#"
when hovering on button footer shows this :
and inspect element shows this :
I tried with other account but that shows this layer as first thing after login :
Now I have question how to click on these 2 types of button via splinter or selenium :
first type of button which shows "#" as href
second which chrome shows for block, allow things
My code is :
from selenium import webdriver
from splinter import Browser
web_driver=webdriver.Chrome('/Users/paul/Downloads/chromedriver/chromedriver')
url = "https://www.example.com"
browser = Browser("chrome")
visit_browser = browser.visit(url)
email_box = '//*[#id="email"]'
find_1 = browser.find_by_xpath(email_box)
find_1.fill("example#gmail.com")
password_box = '//*[#id="pass"]'
find_2 = browser.find_by_xpath(password_box)
find_2.fill("example12345")
button_sub = '//*[#id="u_0_5"]'
find_3 = browser.find_by_xpath(button_sub)
find_3.click()
for testing purpose you can try on "see more button" in trending section on facebook, that also shows "#" how to click that ?
Not letting me comment because I don't have enough rep ... but have you tried to select the element by class and then performing .click() on it? That might do the trick as the href being "#" probably means the button has another purpose.
I have solved my problem , since that link was "#" so if i was click via css or other method it was just reloading the page and that layer appear again and again after every reload , But i tried little different solution and i click it by javascript :
First i tried and find the right element for clicking via js console in chrome :
document.getElementsByClassName('layerCancel _4jy0 _4jy3 _517h _51sy _42ft')[0].click();
This is working perfect in js console so now i used splinter method "browser.execute_script()" and pass that script as argument to this method.
browser.execute_script("document.getElementsByClassName('layerCancel _4jy0 _4jy3 _517h _51sy _42ft')[0].click()")
And its working perfect now as i wanted. But still i have not found a way how to click on browser push notification 'Allow" , "Block" etc
Thanks :)

Clicking on a Javascript Link on Firefox with Selenium

I am trying to get some comments off the car blog, Jalopnik. It doesn't come with the web page initially, instead the comments get retrieved with some Javascript. You only get the featured comments. I need all the comments so I would click "All" (between "Featured" and "Start a New Discussion") and get them.
To automate this, I tried learning Selenium. I modified their script from Pypi, guessing the code for clicking a link was link.click() and link = broswer.find_element_byxpath(...). It doesn't look liek the "All" button (displaying all comments) was pressed.
Ultimately I'd like to download the HTML of that version to parse.
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://jalopnik.com/5912009/prius-driver-beat-up-after-taking-out-two-bikers/") # Load page
time.sleep(0.2)
link = browser.find_element_by_xpath("//a[#class='tc cn_showall']")
link.click()
browser.save_screenshot('screenie.png')
browser.close()
Using Firefox with the Firebug plugin, I browsed to http://jalopnik.com/5912009/prius-driver-beat-up-after-taking-out-two-bikers.
I then opened the Firebug console and clicked on ALL; it obligingly showed a single AJAX call to http://jalopnik.com/index.php?op=threadlist&post_id=5912009&mode=all&page=0&repliesmode=hide&nouser=true&selected_thread=null
Opening that url in a new window gets me the comment feed you are seeking.
More generally, if you substitute the appropriate article-ID into that url, you should be able to automate the process without Selenium.

Categories

Resources