Cannot target the element in Python - Selenium - python

I am starting with Python - Selenium and I cannot target the element in login website. I tried many options of targeting this button but no options works (class name, css selector, id, name..). When I skip this step and get on the next page manually by exact url the finding, focusing, sending keys to element and clicking on the "Next" button is no problem but this "welcome" login button cannot target totally.
Using function "driver.find_element_by_XXX".
For example:
"driver.find_element_by_class_name("login-box-actions").click()"
Please, where I am making a mistake?
Many thanks,
regards David
<div class="login-box-actions">
<a style="cursor: pointer" ng-click="confirmLogin()" class="btn btn-primary btn-block btn-flat ng-binding">Login</a>
</div>

It may fail because the element could not found.
For that, you have to wait for the element to be load and clickable.
Refer to the following example, wait for the element to be clickable.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, "btn btn-primary btn-block btn-flat ng-binding"))).click()

You can try finding the element by using different identifiers. Selenium supports:
find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector

You need to click into the <a> element instead of the div:
login_button = driver.find_element_by_xpath("//a[text()='Login']")
login_button.click()

Related

How to click on checkbox filter with selenium Python?

I'm trying to click on a checkbox filter on a website with selenium python. This is a part of its HTML code linked to one of the checkbox options.
<div class="shopee-checkbox" bis_skin_checked="1">
<label class="shopee-checkbox__control">
<input type="checkbox" name="" value="Jabodetabek">
<div class="shopee-checkbox__box" bis_skin_checked="1">
<i> </i>
</div>
<span class="shopee-checkbox__label">Jabodetabek</span>
</label>
</div>
I tried following, but it didn't work.
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://shopee.co.id/search?keyword=baju%20laki-laki')
time.sleep(5)
driver.find_element(By.XPATH, "//input[#value='JABODETABEK']").click()
I read the answers to similar questions, mostly they suggest using 'id' and 'name' to find the element. However, in this input tag, there are only 'type' and 'value'.
Here is one way to select that checkbox:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
[...]
wait = WebDriverWait(driver, 25)
[..]
wait.until(EC.element_to_be_clickable((By.XPATH, '//span[text()="Jabodetabek"]'))).click()
print('clicked on Jabodetabek')
Selenium documentation can be found here.
#value for the checkbox in the xpath is incorrect. The selenium web locator techniques are case sensitive. So make sure to use exact values while identifying objects. Changing your xpath to below would fix the issue.
driver.find_element(By.XPATH, "//input[#value='Jabodetabek']").click()

function click with selenium without id

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

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

Getting "element not visible" using selenium python for SAP EPM browser application

I am trying to automate an SAP EPM Application using Selenium Python. It's a browser based application. I am able to open the home page, after that I have to click on one tile. But I was unable to click it. It says "element not visible".
I tried using xpath, id but no luck.
Tile HTML:
<div class="tile tile-webdyn draggable tileBGColor ui-draggable ui-draggable-handle
ui-droppable border-norm" id="PLANCHGWO" style="position: relative;">
<div class="tileName">
<center>Change PM Order</center>
</div>
<div class="tileImage">
<center>
<img width="50px" height="50px" src="EDWO.png">
</center>
</div>
</div>
You could try invoking a wait, then click the desired WebElement with Javascript to work around the not visible error you are seeing.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# wait for element to exist, then store it in tile variable
tile = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//center[text()='Change PM Order']")))
# click the element with Javascript
driver.execute_script("arguments[0].click();", tile)
If this does not work, we will probably need to see the full page HTML to understand the element you are trying to click. It's possible it is hidden in an iframe or obscured by other elements on the page.

How to locate an element as per the HTML provided through Selenium and Python

I am trying to click a 'next' button with Selenium in Python, which source code is:
<div class="form-group clearfix">
<button id="experiment-method-previous" type="button" class="btn btn-dark pull-left" ng-click="navigate('run',$event)">Previous</button>
<button id="experiment-method-next" type="button" class="btn btn-primary pull-right" ng-click="navigate('export',$event)">Next</button>
</div>
I use the line:
driver.find_element_by_id('experiment-method-next')
but I get this error:
Unable to locate element: [id="experiment-method-next"]
Same for
driver.find_element_by_class_name('btn btn-primary pull-right')
Unable to locate element: .btn btn-primary pull-right
Any thoughts?
As per the HTML you have shared to invoke click() on the element with text as Next you have to induce WebDriverWait for the desired element to be clickable and you can use either of the following solution:
CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-primary.pull-right#experiment-method-next"))).click()
XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-primary pull-right' and #id='experiment-method-next']"))).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
I think your elememnt is inside frame/iframe. To be able to interact with elements inside frame/iframe you have to switch to it's content like this:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//path_to_frame")))
then you can locate your element and interact with it:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "experiment-method-next"))).click()
and when you are done with content inside frame, you have to switch back to default content:
driver.switch_to.default_content()
Note: you have to do some imports:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
PS: if your elements is not inside frame, then just use WebDriverWait:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "experiment-method-next"))).click()
this will wait at least 10 seconds until element will be clickable and the clicks on it. Hope this helps.
The solution I found is to kill first any previous windows my application opens and switch to the latest one:
current_window = driver.window_handles[1]
driver.switch_to_window(current_window)
Now, the Next Button can be clicked as follows:
driver.find_element_by_id('experiment-method-next').click()
Many thanks to the users Andrei Suvorkov and DebanjanB for their help.

Categories