function click with selenium without id - python

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

Related

Selenium unable to locate and click a button

I have tried to make my script click the checkout button with WebDriverWait, XPath, Class, but it doesn't work anyways :
HTML
<div class="confirm-container row">
:before
<div class="col-xs-12">
<button class="btn btn-primary">
<span>Effettua l'ordine</span>
</button>
</div>
::after
</div>
1st try
var = '//*[#id="purchase-app"]/div/div[4]/div[1]/div[2]/div[5]/div/div/button'
WebDriverWait(web, 50).until(EC.element_to_be_clickable((By.XPATH, var)))
web.find_element_by_xpath(var).click()
2nd try
web.find_element_by_class_name('btn btn-primary').click()
3rd try
ActionChains(web).click(web.find_element_by_class_name('btn btn-primary')).perform()
When you are using Explicit wait, try to have a relative xpath :
1st try issue can be resolved by the below code :
WebDriverWait(web, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Effettua l'ordine']/.."))).click()
2nd try web.find_element_by_class_name('btn btn-primary').click() - class name does not accept spaces so, use css selector instead :
web.find_element_by_css_selector('button.btn.btn-primary').click()
same issue with 3rd try.
Update 1 :
The issue is that, the button you are looking is in iframe so you need to switch over it before interaction :
Code :
wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src^='/store/purchase?namespace']")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-primary"))).click()
Imports :
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
You can try this
from selenium.webdriver import ActionChains
ActionChains(browser).click(element).perform()
while element refer to anything between the start tag and end tag.
as stated in the answer here: https://stackoverflow.com/a/61036237/11884764

How do you click an expander button in Python with Selenium?

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.

How do i click on the link while using selenium python

I'm new to python and selenium.
This is an HTML snippet of the page im trying to scrape a page this
I want to scrape on the basis of data-attr1 given in the html snippet.
Please share some code to find this link and click on it.
<a class="js-track-click challenge-list-item" data-analytics="ChallengeListChallengeName" data-js-track="Challenge-Title" data-attr1="grading" data-attr3="code" data-attr4="true" data-attr5="true" data-attr7="10" data-attr-10="0.9" data-attr11="false" href="/challenges/grading"><div class="single-item challenges-list-view-v2 first-challenge cursor"><div id="contest-challenges-problem" class="individual-challenge-card-v2 content--list-v2 track_content"><div class="content--list_body"><header class="content--list_header-v2"><div class="challenge-name-details "><div class="pull-left inline-block"><h4 class="challengecard-title">Grading Students<div class="card-details pmT"><span class="difficulty easy detail-item">Easy</span><span class="max-score detail-item">Max Score: 10</span><span class="success-ratio detail-item">Success Rate: 96.59%</span></div></h4></div></div><span class="bookmark-cta"><button class="ui-btn ui-btn-normal ui-btn-plain star-button" tabindex="0" aria-label="Add bookmark"><div class="ui-content align-icon-right"><span class="ui-text"><i class="js-bookmark star-icon ui-icon-star"></i></span></div></button></span><div class="cta-container"><div class="ctas"><div class="challenge-submit-btn"><button class="ui-btn ui-btn-normal primary-cta ui-btn-line-primary" tabindex="0"><div class="ui-content align-icon-right has-icon"><span class="ui-text">Solved</span><i class="ui-icon-check-circle ui-btn-icon"></i></div></button></div></div></div></header></div></div><div class="__react_component_tooltip place-top type-dark " data-id="tooltip"></div></div></a>
If the page is not loading as fast as expected you can try:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[#data-attr1='grading']"))).click()
You can click on the element using the xpath:
element = driver.find_element_by_xpath("//a[#data-attr1='grading']")
element.click();
Since there is no name id or class available directly you can use xpath.
The element that you are looking for is in div with class challenges-list and you want to click on first link inside it. You can use this xpath
//a[#data-attr1='grading']
And for clicking you can do
driver.find_element_by_xpath("//a[#data-attr1='grading']").click()
Use double-click with Actionchains because click might not work in Python.
eg.
from selenium.webdriver import ActionChains
# Get the element however you want
element = driver.find_element_by_xpath("//a[#data-attr1='grading']")
ActionChains(driver).double_click(settings_icon).perform()

can't click button or submit form using Selenium

I'm trying to use Selenium w/ Python to click on answers to problems posted at a tutoring site, so I can take tests over the command line.
I enter the following:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver.get('https://www.varsitytutors.com/sat_critical_reading_diagnostic_1-problem-10821')
(an annoying popup comes up at this point -- we can ignore that for now)
Answers on the page are embedded in forms like this:
<div class="question_row">
<form class="button_to" method="post" action="/problem_question_answers/save_answer?answer_id=539461&problem_id=5065&qotd=false&question_id=10821">
<input id="answer_539461" class="test_button" type="submit" value="select" /><input type="hidden" name="authenticity_token" value="LE4B973DghoAF6Ja2qJUPIajNXhPRjy6fCDeELqemIl5vEuvxhHUbkbWeDeLHvBMtEUVIr7okC8Njp4eMIuU3Q==" /></form>
<div class="answer">
<p>English dramatists refused to employ slang in their work.</p>
</div>
<div style="clear:both"></div>
</div>
My goal is to click an answer such as this one in order to get to the next question using Selenium.
I thought it might be as easy as doing this:
answer_buttons=driver.find_elements_by_class_name('test_button')
answer_buttons[1].click()
But I get error messages saying that the element is out of the frame of the driver.
I've also tried submitting the form, which doesn't produce an error message:
answer_forms=driver.find_elements_by_class_name('button_to')
answer_forms[1].submit()
But it redirects to a different url that doesn't load:
http://www.varsitytutors.com/sat_critical_reading_diagnostic_1-problems-results-d9399f1a-4e00-42a0-8867-91b1c8c9057d
Is there any way to do this programmatically, or is the website's code going to prevent this?
Edit:
With some help I was able to click the button once initially. But an identical submit button (by xpath) for the next question remains unclickable. This is the code I'm presently using:
driver.get('https://www.varsitytutors.com/practice-tests')
# click subject
subject=driver.find_element_by_xpath('/html/body/div[3]/div[9]/div/div[2]/div[1]/div[1]')
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[3]/div[9]/div/div[2]/div[1]/div[1]')))
subject.click()
# select specialty
specialty=driver.find_element_by_xpath('/html/body/div[3]/div[9]/div/div[2]/div[2]/div[1]/div[2]/a[4]')
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[3]/div[9]/div/div[2]/div[2]/div[1]/div[2]/a[4]')))
specialty.click()
# select test
taketest=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[3]/div[8]/div[3]/div[1]/div[1]/a[1]')))
driver.execute_script("arguments[0].click();", taketest)
# click away popup
button=WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(.,'No Thanks')]")))
button.location_once_scrolled_into_view
button.click()
# select first choice
choice=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[3]/div[7]/div[1]/div[3]/div[1]/form/input[1]')))
driver.execute_script("arguments[0].click();", choice)
I repeat this code again over the next few lines. It has no effect, however; the drive stays on question two and the next few clicks don't work...
choice=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[3]/div[7]/div[1]/div[3]/div[1]/form/input[1]')))
driver.execute_script("arguments[0].click();", choice)
choice=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[3]/div[7]/div[1]/div[3]/div[1]/form/input[1]')))
driver.execute_script("arguments[0].click();", choice)
Try the following code.This will handle pop-up and click on the select button.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
driver=webdriver.Chrome()
driver.get('https://www.varsitytutors.com/sat_critical_reading_diagnostic_1-problem-10821')
driver.maximize_window()
button=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(.,'No Thanks, Start The Test')]")))
button.location_once_scrolled_into_view
button.click()
eleQuestions=WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'input.test_button')))
driver.execute_script("arguments[0].click();", eleQuestions[2])
button=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(.,'No Thanks')]")))
button.location_once_scrolled_into_view
button.click()
Please note: you can change the indexes from 2 to 6.
Snapshot:
If you would like to select any particular Question as you mentioned then try below code.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
driver=webdriver.Chrome()
driver.get('https://www.varsitytutors.com/sat_critical_reading_diagnostic_1-problem-10821')
driver.maximize_window()
button=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(.,'No Thanks, Start The Test')]")))
button.location_once_scrolled_into_view
button.click()
eleQuestions=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//div[./p[text()='English dramatists refused to employ slang in their work.']]/parent::div//input[1]")))
driver.execute_script("arguments[0].click();", eleQuestions)
button=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(.,'No Thanks')]")))
button.location_once_scrolled_into_view
button.click()

How to click on the button when the textContext contains leading and trailing white-space characters?

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

Categories