Nothing happend when clicking a button in selenium - python

from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get('https://www.worldometers.info/coronavirus/country/canada/')
time.sleep(1)
button = driver.execute_script("window.scrollTo(0, 5500)")
button1 = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div[1]/div[8]/div/div[9]/a/button')
button1.click()
Trying to click this button using xpath but it just doesn't seem to click the button to extend the webpage. I first tried to copy by xpath which didn't work and then I copied the full xpath which also didn't work. After that, I tried to find by name and enter the text on the button and click the button. But none of these methods actually worked and clicked the button. So how can I click this button so that it extends the webpage allowing me to scrape more of the webpage?

The problem was that the button wasn't at "clickable state" despite was rendered in page at moment.
To obviate this, you have to add an explicit wait to tell the driver to wait until it's state change to clickable.
Try to use this code instead :
from selenium import webdriver
import time
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()
driver.get('https://www.worldometers.info/coronavirus/country/canada/')
time.sleep(1)
button = driver.execute_script("window.scrollTo(0, 5500)")
button1 = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[4]/div[2]/div[1]/div[8]/div/div[9]/a/button')))
button1.click()

To click on View more News Induce WebdriverWait and wait for presence_of_element_located() and following css selector.
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
driver = webdriver.Chrome()
driver.get('https://www.worldometers.info/coronavirus/country/canada/')
button=WebDriverWait(driver,20).until(EC.presence_of_element_located((By.CSS_SELECTOR,"a.load-more__btn>.btn")))
button.location_once_scrolled_into_view
button.click()
Or Use following xpath
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
driver = webdriver.Chrome()
driver.get('https://www.worldometers.info/coronavirus/country/canada/')
button=WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH,"//a[#class='load-more__btn']/button[#class='btn']")))
button.location_once_scrolled_into_view
button.click()

Related

Selenium Python find_element Xpath cant find xpath

I want to access an website with selenium and than a addblock-window appears, in which i need to click a button for it to disappear.
Eventhough I can find my XPath(//button[#title='Einverstanden'], /html/body/div/div[2]/div[3]/div[1]/button[#title = 'Einverstanden'] or
//button[contains(text(),"Einverstanden")]') in the browser,
I can't find it with my Python script. And i can't seem to find the mistake.
Here 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
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.get("``https://www.derstandard.at/story/2000134260361/endspiel-vor-gericht-prozess-gegen-boris-becker-startet-in-london``")
driver.maximize_window()
x = driver.find_element(By.XPATH, "//button[#title = 'Einverstanden']")
print(x)
This is the error I'm getting.
The button is enclosed in an iframe in which case, you first need to switch to the iframe and then access the element
This should work:
driver.get("https://www.derstandard.at/consent/tcf/")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//div[contains(#id, 'sp_message_container')]//iframe")))
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[title='Einverstanden']"))).click()
Wait Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
In case you want to switch to default frame again, you may use this when required:
driver.switch_to.default_content()

Selenium on Twitter with Python. I can't get the button with any locating elements of selenium

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()

Selenium Automate Clicking "We value yout privacy" Pop-Up

I am using Selenium with Python on a webpage with JavaScript. The script runs until a pop=up asks for a click either to agree or seek More Options labels appear. The HTML for the pop-up disappears if an option is manually clicked. Would appreciate some guidance on how to click "Agree" automatically.
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
driver = webdriver.Chrome(executable_path='C:/A38/chromedriver_win32/chromedriver.exe')
driver.implicitly_wait(30)
driver.maximize_window()
# Navigate to the application home page
driver.get("https://www.sportinglife.com/racing/results/2020-11-23")
To click on the button with text AGREE. First induce waits for page load and then wait for the element to be clickable.
driver.get('https://www.sportinglife.com/racing/results/2020-11-23')
wait=WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='AGREE']"))).click()
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
This will do the trick:
driver.find_element_by_xpath('//*[#id="qc-cmp2-ui"]/div[2]/div/button[2]').click()

Login Button can not be found with selenium

https://www.sevenonemedia.de/tv/programm/programmwochen
Here I want to login:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
options = Options()
chrome_path = "T:/Markus/WebScrapingExample/Chromedriver/chromedriver.exe"
driver = webdriver.Chrome(executable_path=chrome_path,chrome_options=options)
driver.set_window_size(1280, 720)
time.sleep(5)
driver.get("https://www.sevenonemedia.de/tv/programm/programmwochen")
driver.find_element_by_id("_58_login").send_keys("name")
driver.find_element_by_id("_58_password").send_keys("pw")
driver.find_element_by_xpath('//*[#id="sign-in-button"]').click()
ElementNotInteractableException: element not interactable
(Session info: chrome=78.0.3904.97)
This is my error
Id is there. Why does this happen?
This page contains duplicate of element with ID sign-in-button. If your selector points to more than one element, driver always takes the first from the top of the DOM one which is not interactable in this case. You must refer to the second element with this id. Try this selector for "Sign in" button:
//*[#id="aheadcustom_p_p_id_58"]//button
hi first things you should not give your password to all of the stackoverflow community :)
you can't click on the button because there is a popup at the bottom of the page and you have to click on it first for selenium it's hidding your button
last is that the totality of your code ?
if yes you forgot
driver = webdriver.Firefox() #or any other webdriver
you have not create driver without this line
EDIT !!
it wasn't working with only the modification above but with this one its good
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
time.sleep(5)
driver = webdriver.Firefox()
driver.get("https://www.sevenonemedia.de/tv/programm/programmwochen")
driver.find_element_by_id("_58_login").send_keys("login")
driver.find_element_by_id("_58_password").send_keys("pssd")
driver.find_element_by_xpath("/html/body/div[1]/div/div/div[2]/a").click()
driver.find_element_by_css_selector("#_58_fm > fieldset:nth-child(1) > div:nth-child(6) > button:nth-child(1)").click()
this work :)
sometime css selector a safer and work better
To click on the Login button you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using XPATH:
driver.get("https://www.sevenonemedia.de/tv/programm/programmwochen")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#title='Anmelden' and not(contains(#name,'INSTANCE'))]"))).send_keys("a.mai#mediaplus.com")
driver.find_element_by_xpath("//input[#title='Passwort' and not(contains(#name,'INSTANCE'))]").send_keys("Edidaten17")
driver.find_element_by_xpath("//input[#title='Passwort' and not(contains(#name,'INSTANCE'))]//following::button[1]").click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
The problem is that your button locator isn't unique on the page. It finds two buttons, the first of which is not visible which causes the ElementNotInteractableException.
The simple fix is to use the CSS selector below
#main-content #sign-in-button
That will find only the button you want. So your last few lines of code would be
driver.find_element_by_id("_58_login").send_keys("name")
driver.find_element_by_id("_58_password").send_keys("pw")
driver.find_element_by_css_selector('#main-content #sign-in-button').click()
sleep(1)
login_box = driver.find_element_by_name('login')
login_box.click()
this is for facebook login button, just inspect the website and see the id/name/type to make your code automate to work properly

Can not find/click this button with Selenium/Firefox

I am trying to press this "New Search" button. It appears on the top of the screen after entering a search on http://www.lexisnexis.com/hottopics/lnacademic/
I have looked at the Xpath and Unique Selector.
What I have tried:
browser.find_element_by_css_selector('#restoreButtons > a:nth-child(3)').click()
browser.find_element_by_xpath(id('restoreButtons')/x:a[3])
browser.find_element_by_xpath(/x:a[3])
For all three I get an "unable to locate element error"
To build on alecxes answer,
once you're inside the iframe, you can find the clickable using it's xpath:
new_search_xpath = '/html/body/div[2]/table/tbody/tr[2]/td[2]/div[1]/table/tbody/tr/td/span/a[3]'
new_search = driver.find_element(By.XPATH, new_search_xpath)
new_search.click()
You should consider installing firebug firefox addon to grab Xpaths:
https://addons.mozilla.org/en-US/firefox/addon/firebug/
This is because the element is inside an iframe. You have to be in the context to search elements inside. Use .switch_to.frame():
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 10)
frame = wait.until(EC.presence_of_element_located((By.ID, "mainFrame")))
driver.switch_to.frame(frame)
FYI, here is the complete working code:
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('http://www.lexisnexis.com/hottopics/lnacademic/')
actions = ActionChains(driver)
wait = WebDriverWait(driver, 10)
frame = wait.until(EC.presence_of_element_located((By.ID, "mainFrame")))
driver.switch_to.frame(frame)
driver.find_element_by_id("terms").send_keys("Test")
driver.find_element_by_id("srchButt").click()

Categories

Resources