Is it possible to use selenium to click this button? - python

The xpath is correct and I've tried both variations but it returns NoneType
from selenium import webdriver
from bs4 import BeautifulSoup
import time
import pyautogui
# Set up a webdriver instance using the Chrome browser
driver = webdriver.Chrome()
# Navigate to the staking page
driver.get('https://staking.chain.link/')
# Wait for the page to load
time.sleep(15)
# Find the "Next" button
next_button = driver.find_element_by_xpath('/html/body/div[1]/main/div/div[3]/div[3]/button[2]')
# Click the "Next" button to retrieve the next 10 results
next_button.click()
I want to be able to click through the table displaying the oracles answers. Is it possible to use Selenium to click this button?
I also tried:
next_button = class_element= driver.find_element_by_class('button_secondary__WYcyn paragraph-100')

The find_element_by_xpath has been deprecated in newer versions of Selenium
add this import
from selenium.webdriver.common.by import By
change this line
next_button = driver.find_element(By.XPATH, '/html/body/div[1]/main/div/div[3]/div[3]/button[2]')
https://selenium-python.readthedocs.io/

Related

Selenium Python is unable to locate an element

I'm new to webdrivers and am experimenting with them.
I'm trying to click a button when opening a webpage and it keeps giving the error of unable to locate element.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("html page")
button = driver.find_element(By.ID, "onetrust-accept-btn-handler")
button.click()
i have tried id and xpath but i dont know what else to use.
the path for the button is:
/html/body/div[3]/div[3]/div/div/div[2]/div/div/button
<button id="onetrust-accept-btn-handler" tabindex="0">Run</button>
You can use implicit wait for wait until the page is fully loaded
driver.implicitly_wait(10)

Expand Button Selenium Python

import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# FIRST - GET THE WEBPAGE
url = "https://op.europa.eu/en/web/who-is-who/organization/-/organization/EP/EP"
driver = webdriver.Chrome("C:/Users/XXX/Downloads/chromedriver_win32/chromedriver.exe")
driver.get(url)
# SECOND - CLICK THE BUTTONS
test = driver.find_element_by_css_selector("op-icon op-icon-more tree-hitarea").click()
print(test)
Now my question is how to be able to use the .click() in selenium to extend this button/icon. Although I find some similar problems, it is never about an icon. I am at loss
driver.find_element_by_css_selector("span.op-icon.op-icon-more.tree-hitarea").click()
Multiple class names in css selector or seperated by a .
to expand the button you will needed to use the xpath of element and than to click it.
Install the chrome addon XPath Helper from chrome and then after refresh click the addon and then shift + click on the element what you neded.
After that use driver.find_element_by_xpath().click()

selenium can't locate the element while the element is right there

i am trying to using the selenium auto input the HTML code in http://ueditor.baidu.com/website/examples/completeDemo.html. My procedure is that click the html first, and then code HTML in, while the IDE always told me that cant locate the element. it mad me crazy that the after click the HTML Button, the element is right there, but always error on. i just wonder how can i write in box after click the HTML button by using selenium? thanks to the warm-hearted guy
import os,time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
chromePath = r'E:/Python/WEB/web-infor-transfer/monidenglu/chromedriver.exe'
wd = webdriver.Chrome()
loginUrl = 'http://ueditor.baidu.com/website/examples/completeDemo.html'
wd.get(loginUrl)
wd.find_element_by_xpath('//*[#id="edui4"]').click()
time.sleep(2)
wd.find_element_by_xpath('//*[#id="edui1_iframeholder"]/div/div[2]/div/div/div[2]/div/div[2]').send_keys('hello')
time.sleep(5)
wd.quit()
Try action chains. For example - sending keys to the browser itself worked:
wd.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div/div[2]/div/div/div[2]/div/div[2]/pre[2]/span').click()
actions = ActionChains(wd)
actions.send_keys('hello')
actions.perform()

Click sub-element button when it appears after clicking another button with Python Selenium?

I'm having trouble with the Selenium webdriver's click() feature in Python when I'm trying to click on buttons that only appear after you click on a parent button.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
url = "https://law.lexmachina.com/cases/?pending-from=2000-01-01&pending-to=2000-02-01&filters=true&tab=summary&view=analytics&cols=475"
driver.get(url)
driver = webdriver.Chrome()
elem0 = driver.find_element_by_id('export-icon-container') # this works
all_children_by_css = elem0.find_elements_by_css_selector("*") # this works, but doesn't click on the sub-button (XLS) one successfully when I run below...
all_children_by_css[0].click() # this just makes the parent button's little window appear and disappear, the same as elem0.click() does.
>>> all_children_by_css[0] # this is the webElement that I thought was for the XLS button
<selenium.webdriver.remote.webelement.WebElement (session="6b4a559408fa4d512f8596759d81eaf7",
element="d83be2ca-c879-4706-85ef-db7120d345a3")>
Basically, I want to export the XLS file via the Webdriver, so that later on I can do this on a loop with URLs of filters of the data.
I included annotated screenshots below, detailing the buttons I'm trying to click & the inspected code associated with them.
I would recommend to see if there an API on the page to export directly. if not, You would probably need dynamic wait. you can try something like below.
from selenium import webdriver
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
driver = webdriver.Chrome()
url = "https://law.lexmachina.com/cases/?pending-from=2000-01-01&pending-to=2000-02-01&filters=true&tab=summary&view=analytics&cols=475"
driver.get(url)
#Click on Export Icon
elem0 = driver.find_element_by_id('export-icon-container').click()
#Wait for XLS option to show up
wait = WebDriverWait(driver, 10)
ExportOption= wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#data-action='export-item'][contains(text(),'XLS')]")))
ExportOption.click()

Python Selenium click on element by xpath

I can locate the XPATH of the element that I want, however it will not allow me to click on it. In specific, it throws a "WebDriverException."
from selenium import webdriver
browser=webdriver.Chrome()
url='https://fred.stlouisfed.org/categories/32261'
browser.get(url)
click=browser.find_element_by_xpath("//a[#title='next page']")
print(click.get_attribute('title'))
click.click()
Returns the following error:
You cannot click on required element because it's not visible currently. You should scroll down to "Next" button before clicking it:
from selenium import webdriver
browser = webdriver.Chrome()
url = 'https://fred.stlouisfed.org/categories/32261'
browser.get(url)
next_button = browser.find_element_by_xpath("//a[#title='next page']")
browser.execute_script("arguments[0].scrollIntoView();", next_button)
next_button.click()
So, the XPath was there, however I don't believe I was actually pointed to it when trying the initial "click.click()." There might be a better solution, however this seems to be working for now.
from selenium import webdriver
browser=webdriver.Chrome()
url='https://fred.stlouisfed.org/categories/32261'
browser.get(url)
click=browser.find_element_by_xpath("//a[#title='next page']")
print(click.get_attribute('title'))
click.send_keys('next page')
click.click()

Categories

Resources