This is my 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
browser = webdriver.PhantomJS()
browser.set_window_size(1120, 550)
browser.get("http://www.jamiiforums.com/kenyan-news/225589-kenyan-and-tanzanian-surburbs.html")
username = browser.find_element_by_id("navbar_username")
password = browser.find_element_by_name("vb_login_password_hint")
username.send_keys("user")
password.send_keys("password")
browser.find_element_by_class_name("loginbutton").click()
wait = WebDriverWait(browser, 10)
wait.until(EC.visibility_of_element_located((By.XPATH, '//h2[contains(., "Redirecting")]')))
wait.until(EC.title_contains('Kenyan & Tanzanian'))
link = browser.find_element_by_xpath('//div[#class="vbseo_liked"]/a[contains(#onclick, "return vbseoui.others_click(this)")]')
link.click()
browser.save_screenshot('screenie.png')
print 'success!!'
browser.close()
For this HTML code:
<div class="vbseo_liked">
Nyaralego
,
Sikonge
,
Ab-Titchaz
and
<a onclick="return vbseoui.others_click(this)" href="http://www.jamiiforums.com/kenyan-news/225589-kenyan-and-tanzanian-surburbs.html#">11 others</a>
like this.
</div>
I want to be able to click on this link:
<a onclick="return vbseoui.others_click(this)" href="http://www.jamiiforums.com/kenyan-news/225589-kenyan-and-tanzanian-surburbs.html#">11 others</a>
And then take a screenshot of the page after it has been clicked. This error I keep getting though when i run the code.
selenium.common.exceptions.NoSuchElementException: Message: {"errorMessage":"Unable to find element with class name 'vbseo_liked'"
You need to wait for the list of posts to load before making a click:
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
browser = webdriver.PhantomJS()
browser.maximize_window()
browser.get("http://www.jamiiforums.com/kenyan-news/225589-kenyan-and-tanzanian-surburbs.html")
username = browser.find_element_by_id("navbar_username")
password = browser.find_element_by_name("vb_login_password_hint")
username.send_keys("username")
password.send_keys("password")
browser.find_element_by_class_name("loginbutton").click()
wait = WebDriverWait(browser, 10)
wait.until(EC.visibility_of_element_located((By.XPATH, '//h2[contains(., "Redirecting")]')))
wait.until(EC.title_contains('Kenyan & Tanzanian'))
wait.until(EC.visibility_of_element_located((By.ID, 'postlist')))
link = browser.find_element_by_xpath('//div[#class="vbseo_liked"]/a[contains(#onclick, "return vbseoui.others_click(this)")]')
link.click()
browser.save_screenshot('screenie.png')
print 'success!!'
browser.close()
Note that the generated screenshot would be very large (about 39 MB on disk).
Related
I'm trying to use Selenium to access data on the next page. For some reason, I can't get it to click submit on the web page: https://www.clarkcountycourts.us/Portal/Home/Dashboard/29
my code is as followed:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(options=options)
driver.implicitly_wait(20) # gives an implicit wait for 20 seconds
driver.get("https://www.clarkcountycourts.us/Portal/Home/Dashboard/29")
search_box = driver.find_element_by_id("caseCriteria_SearchCriteria")
search_box.send_keys("Robinson")
WebDriverWait(driver, 15).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name^='a-'][src^='https://www.google.com/recaptcha/api2/anchor?']")))
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//span[#id='recaptcha-anchor']"))).click()
submit_box = driver.find_element_by_id("btnSSSubmit").click()
I get the error
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="btnSSSubmit"]"}
for the last line of code submitting any assistance would be greatly appreciated.
the button elements are as followed:
<input name="Search" id="btnSSSubmit" class="btn btn-primary pull-right" value="Submit" type="submit">
In your code you are switching into the iframe and accessing an element inside it.
But the submit button is not inside that iframe, so to continue with elements out of that iframe you have to switch to the default content.
This should work better:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(options=options)
driver.implicitly_wait(20) # gives an implicit wait for 20 seconds
driver.get("https://www.clarkcountycourts.us/Portal/Home/Dashboard/29")
search_box = driver.find_element_by_id("caseCriteria_SearchCriteria")
search_box.send_keys("Robinson")
WebDriverWait(driver, 15).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name^='a-'][src^='https://www.google.com/recaptcha/api2/anchor?']")))
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//span[#id='recaptcha-anchor']"))).click()
driver.switch_to.default_content()
submit_box = driver.find_element_by_id("btnSSSubmit").click()
I am trying to click spam button with Selenium on Python. But when I go to three dotes on someone profile i can click report #thisperson then I can't click "It’s suspicious or spam" button. I'm going crazy because I tried every way for pulling button with selenium. For example, find_element_by_id, path, css selector, class name and They don't work. Please help me,show me a way, enlighten me.
My Python code for clicking button:
spam_button = browser.find_element_by_xpath('//form[#id="report_webview_form"]/button[2]/span')
here is html tags for this button :
I tried a complicated path for clicking button :
spam_button = browser.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div[2]/div/iframe/#document/html/body/div/div/form/button[2]/span')
I don't know how I write "document" tag. So I get the error.
I tried find_element_by_id:
spam_button = browser.find_element_by_id('spam-btn')
browser.implicitly_wait(5)
ActionChains(browser).move_to_element(spam_button).click(spam_button).perform()
And I got the error "Unable element"
I am lost. How can I click the button with selenium ?
My whole code:
import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
browser = webdriver.Chrome(ChromeDriverManager().install())
url ="https://twitter.com/ANYACCOUNT"
browser.get(url)
time.sleep(1)
browser.find_element_by_xpath("//a[#href='/login']").click()
time.sleep(1)
username = browser.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[1]/label/div/div[2]/div/input')
time.sleep(1)
username.send_keys("MYNICKNAME")
time.sleep(1)
password = browser.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[2]/label/div/div[2]/div/input')
time.sleep(1)
password.send_keys("MYPASSWORD")
time.sleep(1)
browser.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[3]/div/div/span/span').click()
time.sleep(1)
button = browser.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div/div/div/div[2]/div/div/div/div/div/div/div/div/span')
browser.implicitly_wait(5)
ActionChains(browser).move_to_element(button).click(button).perform()
time.sleep(1)
sikayet = browser.find_element_by_xpath('//*[#id="react-root"]/div/div/div/div[2]/div/div/div/div[2]/div[3]/div/div/div/div[5]/div[2]/div/span')
browser.implicitly_wait(5)
ActionChains(browser).move_to_element(sikayet).click(sikayet).perform()
time.sleep(1)
#spam_button = browser.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div[2]/div/iframe/#document/html/body/div/div/form/button[2]/span')
#browser.find_element_by_xpath('//button[#id="spam-btn"]')
#browser.implicitly_wait(5)
#ActionChains(browser).move_to_element(spam_button).click(spam_button).perform()
wait = WebDriverWait(browser,10 )
spam_button = wait.until(EC.element_to_be_clickable((By.ID, "spam-btn")))
time.sleep(1)
spam_button.click()
I tried other ways as mentioned in below comment and I got this error everytime .
Try using explicit wait instead of implicit:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.ID, "spam-btn")))
spam_button = browser.find_element_by_id('spam-btn')
spam_button.click()
OR,
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#spam-btn>div")))
spam_button = browser.find_element_by_css_selector('#spam-btn>div')
spam_button.click()
Update:
You need to switch to iframe.
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
driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
driver.get('https://twitter.com/apiontkovsky/status/1385614545947820037')
wait = WebDriverWait(driver, 15)
# Authorise here
# ...
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".css-1dbjc4n.r-18u37iz.r-15zivkp .css-1dbjc4n.r-xoduu5 svg")))
driver.find_element_by_css_selector(".css-1dbjc4n.r-18u37iz.r-15zivkp .css-1dbjc4n.r-xoduu5 svg").click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Report')]")))
driver.find_element_by_xpath("//span[contains(text(),'Report')]").click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, ".r-1yadl64.r-16y2uox")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#spam-btn>div")))
spam_button = driver.find_element_by_css_selector('#spam-btn>div')
spam_button.click()
<input type="submit" value="Example" id="free_button_form_button" class="free_swag_element new_button_style profile_page_button_style">
I'm trying to have selenium click the button but that is all the website uses and I don't know how to make it click.
You can try:
from selenium import webdriver
url = "http://websitethatyouwanttoscrape.com"
driver = webdriver.Firefox()
driver.get(url)
button = driver.find_element_by_id('free_button_form_button')
button.click()
Or if you need to wait until the button is clickable, you can:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
url = "http://websitethatyouwanttoscrape.com"
driver = webdriver.Firefox()
driver.get(url)
wait = WebDriverWait(driver, 10)
button = wait.until(EC.element_to_be_clickable((By.ID, 'free_button_form_button')))
ActionChains(driver).click(button).perform()
I am using Python with selenium, and trying to login to the flipkart webpage below is my code and also the html tags. I keep getting "element click intercepted" also my xpath returns null for the username column
xpath used for UN: ("//html[body[div[2][div[div[div[div[div[2][div[form[div[2][input[#type ='text']]]]]]]]]]]]")
from selenium import webdriver
chromedriver = 'E:\chromedriver\chrome\chromedriver.exe'
driver = webdriver.Chrome(chromedriver)
driver.maximize_window()
driver.get ('https://www.flipkart.com/')
driver.implicitly_wait(30)
element=driver.find_element_by_link_text("Login").click()
driver.implicitly_wait(30)
element=driver.find_element_by_xpath("//input[#type ='password']").send_keys("hello")
driver.close()
To simply enter the username and password and enter.
driver.get ('https://www.flipkart.com/')
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Enter Email/Mobile number']/parent::label/parent::div/input"))).send_keys("hello")
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#type ='password']"))).send_keys("hello",Keys.ENTER)
Import
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
I am trying to login to the ESPN website using selenium. Here is my code thus far
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
driver = webdriver.Firefox()
driver.maximize_window()
url = "http://www.espn.com/fantasy/"
driver.get(url)
login_button = driver.find_element_by_xpath("/html/body/div[6]/section/section/div/section[1]/div/div[1]/div[2]/a[2]")
login_button.click()
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div/div/section/section/form/section/div[1]/div/label/span[2]/input")))
except:
driver.quit()
Basically, there are 2 steps, first I have to click the login button and then I have to fill in the form. Currently, I am clicking the login button and the form is popping up but then I can't find the form. I have been using firebug to get the xpath as suggested in other SO questions. I don't really know much about selenium so I am not sure where to look
Try to use
driver.switch_to_frame('disneyid-iframe')
# handle authorization pop-up
driver.switch_to_default_content() # if required
This works for me, switching to the iframe first. Note that you will need to switch back out of the iframe after entering the credentials.
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
driver = webdriver.Firefox()
driver.maximize_window()
url = "http://www.espn.com/fantasy/"
driver.get(url)
login_button = driver.find_element_by_xpath("/html/body/div[6]/section/section/div/section[1]/div/div[1]/div[2]/a[2]")
login_button.click()
iframe = driver.find_element_by_id("disneyid-iframe")
driver.switch_to.frame(iframe)
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div/div/section/section/form/section/div[1]/div/label/span[2]/input")))
element.send_keys("my username")
import time
time.sleep(100)
finally:
driver.quit()