I am working on a script for selenium to open a webpage, and click a series of buttons. The first button is an Expander button, shown in the below image
The HTML code from inspecting the button is below, but the button itself is "class="dojoxExpandoIcon dojoxExpandoIconLeft qa-button-toc"
<div class="dojoExpandoPaneInner">
<div data-dojo-attach-point="titleWrapper" class="dojoxExpandoTitle">
<div class="dojoxExpandoContainer">
<div class="dojoxExpandoIcon dojoxExpandoIconLeft qa-button-toc" data-dojo-attach-point="iconNode" data-dojo-attach-event="ondijitclick:_onIconNodeClick" tabindex="0" aria-controls="slideout"><span class="a11yNode">X</span></div>
</div>
The xpath as well, is as follows:
/html[#class='dj_webkit dj_chrome dj_contentbox has-webkit has-no-quirks svg']/body[#class='claro original']/div[#id='border']/div[#id='slideout']/div[#class='dojoExpandoPaneInner']/div[#class='dojoxExpandoTitle']/div[#class='dojoxExpandoContainer']/div[#class='dojoxExpandoIcon dojoxExpandoIconLeft qa-button-toc']
To find this button, I tried the following code one at a time.
driver.find_element_by_css_selector("div[class^='dojoxExpandoIcon']") # find expander by class name
# driver.find_element_by_css_selector("div[class^='dojoxExpandoIcon dojoxExpandoIconLeft qa-button-toc']") # find expander by class name
# driver.find_element_by_css_selector("div.dojoxExpandoContainer")
However, none of them have worked at all, and result in errors such as
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"div.dojoxExpandoIcon.dojoxExpandoIconLeft.qa-button-toc"}
Is there anything I'm doing wrong here?
Try waiting until the locator is clickable:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 15)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".dojoxExpandoIcon.dojoxExpandoIconLeft.qa-button-toc")))
button = driver.find_element_by_css_selector(".dojoxExpandoIcon.dojoxExpandoIconLeft.qa-button-toc").click()
If the number of classes is not stable, remove the ones that are changing and leave only stable ones, for example:
.dojoxExpandoIconLeft.qa-button-toc
Here I just removed one of classes.
Read more about waits here https://selenium-python.readthedocs.io/waits.html.
Related
I want know how I can click a button in this page here with selenium the code I try is this
import selenium
from selenium import webdriver
from time import sleep
PATH= "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://yopmail.com/it/")
inputsSI= driver.find_element_by_class_name("md").click()
print(inputsSI)
sleep(100)
driver.close()
the error I get is this:
Message: element not interactable
It's because find_element_* methods will return the first occurrence of the element they find. If you see the HTML page there is another div element before the button that has this "md" class and of course it is not the button you are looking for.
You are looking for this buttun :
<div id="refreshbut">
<button class="md" style="border-radius: 20px;"
title="Controllare la posta #yopmail.com"
onclick="{if(chkl())go()}"><i
class="material-icons-outlined f36"></i></button>
<input type="submit" style="display:none;">
</div>
It is wrapped inside a div with id of "refreshbut".
So all you have to do is first get this div by id. Then search for the element which has the "md" class which is indeed the button you are looking for. (you could also get the button with XPATH. it's up to you)
like:
inputsSI = driver.find_element(By.ID, "refreshbut") \
.find_element(By.CLASS_NAME, "md") \
.click()
Note 1: It's better to use raw-string for your PATH variable.
Note 2: I would put the driver.close() statement inside the finally block so that it always runs
To click on the md button use button.md as a selector then wait for the element to be interactable. I don't know what goes in there so I just added a send keys of a.
wait=WebDriverWait(driver,30)
driver.get("https://yopmail.com/it/")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#login"))).send_keys('a')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button.md"))).click()
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I just started working with Selenium webdriver to try automate clicking a button in a discord chat based on preconditions. The problem I am having is being able to classify this button.
For starters, the HTML code is as follows:
<div tabindex="0" class="reactionInner-15NvIl" aria-label="♥️, press to react" aria-pressed="false" role="button"><img src="/assets/1afd3c799f3e158662a70498e83e2a87.svg" alt="♥️" draggable="false" class="emoji"><div class="reactionCount-2mvXRV" style="min-width: 9px;">1</div></div>
What I first tried to do was to find_element_by_xpath:
driver.find_element_by_xpath('/html/body/div/div[1]/div/div[2]/div/div/div/div[2]/div[2]/div[2]/div[1]/div[1]/div/div/div[48]/div[1]/div[2]/div[2]/div[1]/div/div').click()
But, when a new opportunity for this reaction comes up, the xpath changes:
/html/body/div/div[1]/div/div[2]/div/div/div/div[2]/div[2]/div[2]/div[1]/div[1]/div/div/div[50]/div[1]/div[2]/div[2]/div[1]/div/div
Notice the only part changing it the div[48] to div[50]
The next thing I tried was to find_element_by_class_name:
element = driver.find_element_by_class_name('reactionInner-15NvIl')
driver.execute_script("arguments[0].click();", element)
The reason I did this was because I was having a problem with simply doing:
driver.find_element_by_class_name('reactionInner-15NvIl').click()
That code would give me an error saying Message: element click intercepted: Element <div tabindex="0" class="reactionInner-15NvIl" aria-label="💗, press to react" aria-pressed="false" role="button">...</div> is not clickable at point (405, 94). Other element would receive the click: <span id="NewMessagesBarJumpToNewMessages_122" class="span-3ncFWM">...</span> and the code wouldn't run. The program runs with the execute_script but it just isn't doing anything, the button is not being clicked. So if anyone has any idea how to be able to click this button, any help would be appreciated!
You can use xpath in a better way than navigate between the divs, like you did the first time.
First of all, if you can change the HTML to use id, or name, thats better.
In this case, you could use this xpath:
//div[#class = "reactionInner-15NvIl"]
When searching by class, this could return multiple results, so use the function find_elements_by_xpath, and then choose the exactly one
Induce WebDriverWait and wait for element_to_be_clickable() and following css selector.
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div[class*='reactionInner-'][role='button']"))).click()
You need to import following libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Update:
Induce Javascript Executor to click.
driver.execute_script("arguments[0].click();", WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div[class*='reactionInner-'][role='button']"))))
I have a problem navigating a website using selenium. This is my code:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://webtrader.binary.com/v2.2.8/main.html#')
resources = driver.find_element_by_id('ui-id-1')
resources.click()
However, I get the exception:
selenium.common.exceptions.ElementNotInteractableException: Message: Element <ul id="ui-id-1" class="ui-menu ui-widget ui-widget-content ui-menu-icons"> could not be scrolled into view
I don't understand where I went wrong. I am trying to access 'Historical data' from the dropdown menu labeled "Resources". Could someone please help me access it. Maybe I got the id for Resources wrong. You could also check that out.
The element you want to click to open the dropdown is the previous sibling of the element resources
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.select import By
from selenium.webdriver.support.ui import WebDriverWait
wait = WebDriverWait(driver, 10)
driver.find_element_by_class_name('resources').click()
historical_data = wait.until(ec.visibility_of_element_located((By.ID, 'ui-id-4')))
There are many things happening here. First of all, your code is lacking a wait. Without it will always fail, as the page is dynamically loaded. Read about Waits.
Secondly, here resources = driver.find_element_by_id('ui-id-1') you are finding the element from the dropdown menu, and then you are trying to click it. But the dropdown menu is not opened. You should click on it, then wait for the option to appear, only then click on the 'Historical data'.
I'm trying to press a button with selenium in python. I am not able to locate it with xpath or css selector.
For the rest of the things in the script I have been using xpath and it works fine, but this button's xpath seems to be relative each time I open it. Therefore I have tried to access is with the css selector.
The element I am trying to access looks like this:
<button type="button" data-dismiss="modal" class="btn btn-primary pull-right">Opret AnnonceAgent</button>
The css selector from inspect in chrome:
#\35 d167939-adc2-0901-34ea-406e6c26e1ab > div.modal-footer > div > button.btn.btn-primary.pull-right
Let me know if I should post more html.
I have tried many stack oveflow questions, and tried many variatiosn of the css selector, like putting it as:
driver.find_element_by_class_name('#\35 d167939-adc2-0901-34ea-406e6c26e1ab.div.modal-footer.div.button.btn.btn-primary.pull-rightdiv.button.btn.btn-primary-pull-right').click()
driver.find_element_by_class_name('div.button.btn.btn-primary-pull-right').click()
driver.find_element_by_class_name('button.btn.btn-primary-pull-right').click()
driver.find_element_by_class_name('btn.btn-primary-pull-right').click()
driver.find_element_by_class_name('btn-primary-pull-right').click()
I have also tried a sleep timer.
The button is in a window that opens when you press the previous button, with the background greyed out, if that helps. Picture.
# This opens up the window in which to press the next button (works fine)
button = driver.find_element_by_xpath('//*[#id="content"]/div[1]/section/div[2]/button')
button.click()
driver.implicitly_wait(15)
time.sleep(5)
# This is what doesn't work
driver.find_element_by_class_name('button.btn.btn-primary-pull-right').click()
I expect for the program to press the button and it doesn't, it just sits there.
The desired element is within a Modal Dialog Box, so you need to induce WebDriverWait for the desired element to be clickable and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-primary.pull-right[data-dismiss='modal']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-primary pull-right' and #data-dismiss='modal'][text()='Opret AnnonceAgent']"))).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
class_name recives one class as parameter, but you are using css syntax as locator.
With css_selector
driver.find_element_by_css_selector('button.btn.btn-primary-pull-right')
Which means <button> tag with 2 classes, btn and btn-primary-pull-right
With class_name
driver.find_element_by_class_name('btn-primary-pull-right')
My guess was right.There was an iframe which stopping to access the element.You have to switch to iframe first to access the button element.Try now with following 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
#WebDriverWait(driver,20).until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH,'//iframe[starts-with(#id,"rdr_")]')))
WebDriverWait(driver,20).until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH,'//iframe[#id="qualaroo_dnt_frame"]')))
driver.find_element_by_css_selector('button.btn.btn-primary').click()
I'm trying to click on the following button using Selenium with python:
<button type="submit" tabindex="4" id="sbmt" name="_eventId_proceed">
Einloggen
</button>
This is just a simple button which looks like this:
Code:
driver.find_element_by_id('sbmt').click()
This results in the following exception:
selenium.common.exceptions.ElementNotInteractableException: Message:
Element <button id="sbmt" name="_eventId_proceed" type="submit">
could not be scrolledinto view
So, I tried scrolling to the element using ActionChains(driver).move_to_element(driver.find_elements_by_id('sbmt')[1]).perform() before clicking the button.
(Accessing the second element with [1] because the first would result in selenium.common.exceptions.WebDriverException: Message: TypeError: rect is undefined exception.).
Then I used
wait = WebDriverWait(driver, 5)
submit_btn = wait.until(EC.element_to_be_clickable((By.ID, 'sbmt')))
in order to wait for the button to be clickable. None of this helped.
I also used driver.find_element_by_xpath and others, I tested it with Firefox and Chrome.
How can I click on the button without getting an exception?
Any help would be greatly appreciated
To invoke click() on the element you need to first use WebDriverWait with expected_conditions for the element to be clickable and you can use the following solution:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='sbmt' and normalize-space()='Einloggen']"))).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