I was making a bot with selenium but I can not click here:
There are many classes and I can not find which one I should use , because I tried something but didn't do it.
For example, button name is OTURUM AÇ and I wrote like this , but it does not work.
browser = driver.find_elements_by_xpath('//button[text()="OTURUM AÇ"]').click()
Update from comments: https://dlive.tv/ I wanna log in there. But I can't.
This should work for you
btn = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='OTURUM AÇ']")))
driver.execute_script('arguments[0].click();', btn)
It doesn't work because you are trying to find a button which has a text 'OTURUM AÇ' inside of it. In html which you add to question, there is no <button> tag so you need to search this element with <span>.
login = driver.find_element_by_xpath('//span[text()="OTURUM AÇ"]')
driver.execute_script('arguments[0].click();', login)
In html, it is not mandatory to use a <button> tag to place a button on page so make sure you check the tags.
You use wrong locator. There is login button with span tag, not button.
Locotor will be:
//span[text()="OTURUM AÇ"]
But using text in locators is not good practice. You can rewrite it using css:
.sign-register-buttons .btnA span
or the same xpath locator:
//*[contains(#class, 'sign-register-buttons')]//*[contains(#class, 'btnA')]//span
They will work for any site language.
To click on the element with text as OTURUM AÇ you can use the following xpath based Locator Strategies:
browser.find_element_by_xpath("//div[#class='sign-register-buttons flex-align-center']//span[text()='OTURUM AÇ']").click()
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='sign-register-buttons flex-align-center']//span[text()='OTURUM AÇ']"))).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
Related
I have an element that clears a query after the query is made, but I can't get selenium to locate it.
Here is the xpath for the element:
/html/body/main/div/section[1]/div/a[2]
This is the html for the element:
<a onclick="onClearClicked()" class="btn btn--secondary btn--sm">clear</a>
Things that I have tried:
Finding by using the whole xpath, css selector, class name, text
Using css to find all buttons on the page and iterating over them until I find the right one (it doesn't find any buttons with text)
buttons = mydriver.find_elements_by_css_selector("button")
for button in buttons:
print(button.text)
if button.text.strip() == "clear":
button.click()```
Exiting the iframe I was in before and using the full xpath
driver.switch_to().defaultContent()
I have a work around that involves quitting the driver and reopening it for every query, but this would involve logging in and navigating to the right page every time and I'd much rather be able to just use the clear button.
Your html doesn't have any "button" element; it's an "a" (hyperlink) element. So, you should do:-
buttons = mydriver.find_elements_by_css_selector("a")
Using full xpath not a good way , so try the following code:
buttons =driver.find_elements_by_css_selector("[class='btn btn--secondary btn--sm']")
for button in buttons:
print(button.text)
if button.text.strip() == "clear":
button.click()
driver.find_element(By.XPATH,"//a[.='clear']").click()
If you want to click the element with the text clear you can just look for an a tag with that text.
The element is a <a> tag which have the innerText as clear
<a onclick="onClearClicked()" class="btn btn--secondary btn--sm">clear</a>
Solution
To click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "clear"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn--secondary.btn--sm[onclick^='onClearClicked']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='btn btn--secondary btn--sm' and starts-with(#onclick, 'onClearClicked')]"))).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 have been trying to make a Selenium bot that searches for a word on github, clicks on the first link and then downloads it with Python 3.8 and I got stuck with making the bot click on the hyperlink. I understand that I can make the bot click on it with driver.find_element(By.XPATH, "Xpath").click() but I want to be able to find the path of the href with another method for the sake of learning, in this case CSS_SELECTOR. Source code of the first hyperlink result is like this:
HTML:
Since every single result is under the same "a" selector with a class of "v-align-middle", I thought of using this code: driver.find_element(By.CSS_SELECTOR, ".v-align-middle").click() but it did not seem to work. What am I missing?
The desired element is a dynamic element, so to click() on the element you need to induce WebDriverWait for the 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, "a.v-align-middle[href='/asdf-vm/asdf'][data-hydro-click][data-hydro-click-hmac]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='v-align-middle' and #href='/asdf-vm/asdf'][#data-hydro-click and #data-hydro-click-hmac]"))).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
HTML:
<iframe allowpaymentrequest="true" allowtransparency="true" src="https://shopify.wintopay.com/
cd_frame_id_="ca9e4ad6a1559de159faff5c1f563d59"
name="WinCCPay"
id="win-cc-pay-frame"
I'm trying to input text in a CC field. Apparently its in an iframe I picked the last one in the HTML and tried to select it from the identifiers above but I keep getting the element couldn't be found
iframe= wd.find_element_by_id("win-cc-pay-frame")
wd.switch_to.frame(iframe)
The frame is currently being shown in the browser so no need for implicit wait.
To identify the <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#win-cc-pay-frame[name='WinCCPay'][src^='https://shopify.wintopay.com']")))
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='win-cc-pay-frame' and #name='WinCCPay'][starts-with(#src, 'https://shopify.wintopay.com')]")))
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
The problem can be that the name and id of the element are dynamic and change for each unique checkout window? Can you check if adding class attribute at iframe tag and find element by this attribute?
It must be similar to:
iframe = wd.find_element_by_class_name('card-pay-iframe')
wd.switch_to.frame(iframe)
...
wd.switch_to.default_content()
good coding! ¯_(ツ)_/¯
I am trying to create an automated download event for one ERP platform using Selenium in python. This is a bit tricky for this part as there is no specific element to find and click.
Multiple buttons have the same class and no id for the highlighted button (DATABASE/BACKUP)
Can anyone help me with the same?
event flow >>
Login page > click "administration button" > click "database / backup"
thanks
As per Selenium documentation https://selenium-python.readthedocs.io/locating-elements.html you can find all of the elements by their class name and then access them by index ([index_number]).
all_buttons = driver.find_elements_by_class_name("your_class_name")
then you can do something like:
button_to_click = all_buttons[3] # The 4th button which has index 3 in all the retrieved buttons by class name
driver.find_element(By.XPATH,"//a[contains(text(),'Database / Backup')]").click()
Simply click the a tag which contains the text.
To click on the element with text as DATABASE/BACKUP you can use either of the following Locator Strategies:
Using xpath:
driver.find_element(By.XPATH, "//a[#class='menui' and contains(., 'DATABASE/BACKUP')]").click()
Using xpath:
driver.find_element(By.XPATH, "//a[text()[contains(., 'DATABASE/BACKUP')]]").click()
The desired element is a dynamic element, ideally to click on a clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='menui' and contains(., 'DATABASE/BACKUP')]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[text()[contains(., 'DATABASE/BACKUP')]]"))).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
Tried something like this in python unable I want to click on cross using Selenium.
driver.find_element_by_xpath("//span[contains(#onclick, 'parent.$WZRK_WR.closeIframe('60005','intentPreview');')]").click()
As per the HTML you have shared to click on the element depicted as X, first you need to induce WebDriverWait while switching to the <iframe> and again induce WebDriverWait for the desired element to be clickable and you can use the following solution:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='wiz-iframe-intent']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='CT_Interstitial']//span[#class='CT_InterstitialClose']"))).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
Open below URL and Click on Add to Chrome button.
Xpath Helper- https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl?hl=en
Once plugin added to Chrome. Open your application/webpage and press SHIFT+CTRL+X. Black window will appear on top then you can play with xpath.
If xpath is invalid- then you will get xpath error and if no matches found then it will display NULL.
Note- To check element attribute still you can use F12 that is nothing but default inspect element for all browser then check attribute and create your own xpath and try.
Xpath Syntax
// tagname[#attribute-name=’value1′] and if you are not sure about tag name then no worry you can try with * also
//*[#attribute-name='value1']
You are dealing with an iframe. Follow below steps :
You'll need to switch control to iframe.
Then perform your action (in this case 'Click close').
Switch the control back to default frame.
You can try with this css_selector :
div.CT_InterstitialContents+span.CT_InterstitialClose[onclick]
Xpath would be :
//div[#class='CT_InterstitialContents']/following-sibling::span[#class='CT_InterstitialClose' and onclick]
Try to use this xPath:
//div[#id = 'contentDiv']/div/div/span[#class = 'CT_InterstitialClose']
Code:
close_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[#id = 'contentDiv']/div/div/span[#class = 'CT_InterstitialClose']")))
close_btn.click()
Imports:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
Explanation:
WebDriverWait is used to wait until element will be clickable, and only then clicks on it. In this example WebDriverWait will wait at least 10 seconds until element will be clickable.
PS: as I see in the screenshot your element is probably in iframe. That means you have to switch to this iframe first, to be able to interact with the elements in it. The code sample for it would be like this:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "XPATH_TO_FRAME")))
# do stuff
close_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[#id = 'contentDiv']/div/div/span[#class = 'CT_InterstitialClose']")))
close_btn.click()
# switch back to default content
driver.switch_to.default_content()
You can write xpath using class name of span. Please refer an example below.
//span[#class='amountCharged']