Phantomjs click on checkbox - python

I'm using selenium and phantomjs and I would like to learn how to click a checkbox properly. For exemple in this page: https://www.udacity.com/courses/android
I would like to check "Free Courses", so I tried this:
from selenium import webdriver
from selenium.webdriver.common.by import By
def __init__(self):
self.driver = webdriver.PhantomJS(executable_path='/usr/local/bin/phantomjs')
def parse(self, response):
self.driver.get(response.url)
element = self.driver.find_element(By.XPATH, '//div[#class="checkbox"]/label[contains(.,"Free Courses")]')
self.driver.execute_script("arguments[0].click();", element)
The problem is that it doesn't seems to be clicking anything: making a screenshot with self.driver.save_screenshot('screenshot.png') it gives all the results, not filtered.
Is it something I'm doing wrong?

Your xpath locates to label element while you want to click on checkbox element, As I'm seeing in your provided website, there is no need to create xpath to select Free Course checkbox, you can simply find this checkbox using By.NAME as below :-
from selenium import webdriver
from selenium.webdriver.common.by import By
def __init__(self):
self.driver = webdriver.PhantomJS(executable_path='/usr/local/bin/phantomjs')
def parse(self, response):
self.driver.get(response.url)
element = self.driver.find_element(By.NAME, 'Free Course')
element.click()
Note :- Selenium provides click() function to perform click on an element, So there is no need to use execute_script to perform click by javascript if you could simply do this by using click() function.
Hope it helps...:)

Related

How do I click a particular element in a pop up?

I have written the following selenium script:
from selenium import webdriver
PATH= r"C:\Users\David\Desktop\Selenium\chromedriver.exe"
driver=webdriver.Chrome(PATH)
driver.get("https://www.studentbeans.com/uk")
When I enter the website, there is a pop up that appears that asks if I'd like to accept all cookies. I would like to click yes. How do I add this onto my code?
Firstly, you have to select the cookie element using xpath or css selector then click by calling click() function.Remember it that you also need to make full screen using driver.maximize_window()
Try:
import time
from selenium import webdriver
PATH= r"C:\Users\David\Desktop\Selenium\chromedriver.exe"
driver=webdriver.Chrome(PATH)
driver.get("https://www.studentbeans.com/uk")
driver.maximize_window()
time.sleep(4)
cookie_button=driver.find_element_by_xpath('//button[#id="onetrust-accept-btn-handler"]').click()
time.sleep(2)
Inspect the HTML, and find that element. Then copy the xpath or css selector or whatever and:
driver.find_element_by_xpath('copy xpath').click()

Selenium to scroll down a div

How can I scroll down and up inside a div using Selenium? I looked everywhere on the internet. Only solutions for pages.
element = driver.find_elements_by_xpath('//*[#id="root"]/div/main/div/div[2]/div[1]/div[1]/div/div[2]/nav/div[4]/div/div[2]/div/span')
element.execute_script("arguments[0].scrollIntoView();", element )
The actions class is capable of scrolling.
This import:
from selenium.webdriver.common.action_chains import ActionChains
This function:
def ScrollIntoView(element):
actions = ActionChains(driver)
actions.move_to_element(element).perform()
Assuming your element exists and is ready on the page you can Call it as such:
element = driver.find_elements_by_xpath('//*[#id="root"]/div/main/div/div[2]/div[1]/div[1]/div/div[2]/nav/div[4]/div/div[2]/div/span')
ScrollIntoView(element)

Select an input element using Selenium

Inspect
Im trying to click on this button to move to the login page.
my code is :
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://moodle.tau.ac.il/')
thats work fine but i can only find the form by using
loginform = driver.find_element_by_xpath("//form[#id='login']/")
I don't know how to get to the button, it's very basic stuff but I didn't find any good example.
This will click on the login button on moodle.tau.ac.il page.
The line driver.find_element_by_xpath(".//*[#id='login']/div/input").click() finds the login button on the page and clicks it. Xpath is just a selector type that you can use with selenium to find web elements on a page. You can also use ID, classname, and CSSselectors.
from selenium import webdriver
driver = new webdriver.Chrome()
driver.get('moodle.tau.ac.il')
# This will take you to the login page.
driver.find_element_by_xpath(".//*[#id='login']/div/input").click()
# Fills out the login page
elem = driver.find_element_by_xpath("html/body/form/table/tbody/tr[2]/td/table/tbody/tr[1]/td[2]/input")
elem.send_keys('Your Username')
elem = driver.find_element_by_xpath("html/body/form/table/tbody/tr[2]/td/table/tbody/tr[3]/td[2]/input")
elem.send_keys('Your ID Number')
elem = driver.find_element_by_xpath("html/body/form/table/tbody/tr[2]/td/table/tbody/tr[1]/td[2]/input")
elem.send_keys('Your Password')
driver.find_element_by_xpath("html/body/form/table/tbody/tr[2]/td/table/tbody/tr[7]/td[2]/input").click()
The page has two identical login forms and your XPath returns the hidden one.
So with the visible one:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get(r"http://moodle.tau.ac.il/")
driver.find_element_by_css_selector("#page-content #login input[type=submit]").click()
Or with an XPath:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get(r"http://moodle.tau.ac.il/")
driver.find_element_by_xpath("id('page-content')//form[#id='login']//input[#type='submit']").click()
You could find it using XPath as mentioned by #ChrisP
You could find it by CSS selector: "#login input[type='text']"
Or you could also just submit the form... loginForm.submit()
Ideally, you'd have a unique id for that button which would make it very easy to find.

How do I properly click an element in Selenium with Python?

I am trying to click an element, for example "AH", in this page. I use the following code.
from selenium import webdriver
url = "http://www.oddsportal.com/soccer/brazil/serie-a/internacional-santos-vcGTTAKH/"
driver = webdriver.Firefox()
driver.get(url)
element_to_click = driver.find_element_by_link_text("AH")
element_to_click.click()
The problem is that after the element is clicked and the new page is loaded, it goes back to the first page.
Focus the element and call click_and_hold action (worked for me):
from selenium.webdriver import ActionChains
actions = ActionChains(driver)
actions.move_to_element(element_to_click).click_and_hold(element_to_click).perform()
alecxe , that works.
Just to add to the discussion here
So on mouse down it is invoking onClick for the uid(4), when we do a normal click on the element we do not realize that it worked on mouse down not on mouse click.
Thats why when we are using webdriver to do element.click() on it, this does not work and when we use Actions class to simulate mouse down using click_and_hold, It works !!

Splinter or Selenium: Can we get current html page after clicking a button?

I'm trying to crawl the website "http://everydayhealth.com". However, I found that the page will dynamically rendered. So, when I click the button "More", some new news will be shown. However, using splinter to click the button doesn't let "browser.html" automatically changes to the current html content. Is there a way to let it get newest html source, using either splinter or selenium? My code in splinter is as follows:
import requests
from bs4 import BeautifulSoup
from splinter import Browser
browser = Browser()
browser.visit('http://everydayhealth.com')
browser.click_link_by_text("More")
print(browser.html)
Based on #Louis's answer, I rewrote the program as follows:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Firefox()
driver.get("http://www.everydayhealth.com")
more_xpath = '//a[#class="btn-more"]'
more_btn = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_xpath(more_xpath))
more_btn.click()
more_news_xpath = '(//a[#href="http://www.everydayhealth.com/recipe-rehab/5-herbs-and-spices-to-intensify-flavor.aspx"])[2]'
WebDriverWait(driver, 5).until(lambda driver: driver.find_element_by_xpath(more_news_xpath))
print(driver.execute_script("return document.documentElement.outerHTML;"))
driver.quit()
However, in the output text, I still couldn't find the text in the updated page. For example, when I search "Is Milk Your Friend or Foe?", it still returns nothing. What's the problem?
With Selenium, assuming that driver is your initialized WebDriver object, this will give you the HTML that corresponds to the state of the DOM at the time you make the call:
driver.execute_script("return document.documentElement.outerHTML;")
The return value is a string so you could do:
print(driver.execute_script("return document.documentElement.outerHTML;"))
When I use Selenium for tasks like this, I know browser.page_source does get updated.

Categories