<button type="button" name="abc" id="abc" class="bp" onmouseover="this.className = 'bp bph'" onmouseout="this.className = 'bp'" onclick="oCV_NS_.promptAction('finish')" style="font-family:"Arial";font-size:9pt">
<span tabindex="0">GENERATE REPORT</span>
</button>
I want to click this button and tried few codes but nothing worked
tried:
driver.find_element_by_id("abc").click();
driver.find_element(By.ID, "abc")
element_by_name also tried
Please try below code and also check if button is not part of iframe:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome()
wait = WebDriverWait(browser, 10)
button= wait.until(EC.visibility_of_all_elements_located((By.ID, "abc")))
button.click()
Try use with the below xpath:
driver.find_element_by_xpath("//button[#id='abc']/span").click()
Related
Im trying to get this link
<ul class="row">
<li class="col-md-3 col-xs-12 col-sm-6 episodeLink37027" data-lang-key="1" data-link-id="37027" data-link-target="/redirect/37027" data-external-embed="false">
<div class="generateInlinePlayer">
<a class="watchEpisode" itemprop="url" href="/redirect/37027" target="_blank">
<i class="icon VOE" title="Hoster VOE"></i>
<h4>VOE</h4>
<div class="hosterSiteVideoButton">Video öffnen</div>
...
I already tried:
button = driver.find_element_by_css_selector("watchEpisode")
and
button = driver.find_element_by_class_name("a.watchEpisode")
I always get an no such element Exception. Are there other ways to get this element?
Try this
button = driver.find_element_by_css_selector("a.watchEpisode")
or
button = driver.find_element_by_class_name("watchEpisode")
Don't forget to add a delay / wait before accessing this element
UPD
I see no iframes there, but the locator can be improved.
Try this code:
from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
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 as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
CHROMEDRIVER = r"C:\Users\Nico\Desktop\automate_download\chromedriver.exe"
driver = webdriver.Chrome(executable_path=CHROMEDRIVER)
driver.maximize_window()
wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)
driver.get("https://anicloud.io/anime/stream/attack-on-titan")
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[#title="Staffel 1 Episode 1"]'))).click()
button = wait.until(EC.presence_of_element_located((By.XPATH, "//li[not(contains(#style,'none'))]//i[#title='Hoster VOE']/..//div[#class='hosterSiteVideoButton']")))
actions.move_to_element(button).perform()
time.sleep(0.5)
#now you can click this element
button.click()
Try this one, see if it works.
button = driver.find_element_by_xpath("//*[#class='watchEpisode']")
probably you can try with this css selector :
div.generateInlinePlayer>a.watchEpisode
or this xpath:
//div[#class='generateInlinePlayer']//child::a[#class='watchEpisode']
PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
to get the href you can try the below code :
Code trial 1 :
time.sleep(5)
val = driver.find_element_by_xpath("//div[#class='generateInlinePlayer']//child::a[#class='watchEpisode']").get_attribute('href')
print(val)
Code trial 2 :
val = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='generateInlinePlayer']//child::a[#class='watchEpisode']"))).get_attribute('href')
print(val)
Code trial 3 :
time.sleep(5)
button = driver.find_element_by_xpath("//div[#class='generateInlinePlayer']//child::a[#class='watchEpisode'])
val = driver.execute_script("return arguments[0].value", button)
print(val)
Code trial 4 :
time.sleep(5)
button = driver.find_element_by_xpath("//div[#class='generateInlinePlayer']//child::a[#class='watchEpisode']")
ActionChains(driver).move_to_element(button).perform()
print(button.get_attribute('href'))
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
here I have 2 buttons:
Clicked Button:
<button type="button" data-testid="favorite-button" class="styles__button--1wSNn styles__neutral--17MuV styles__elevation--2fhDh styles__elevationMedium--2eus4 styles__circle--3zgIv styles__small--127Kw"><span class="styles__iconAfter--3xNI0"><div class="FavoriteIcon__icon--2fuH8 FavoriteIcon__small--2hXns FavoriteIcon__favorited--zicAG"><img class="styles__image--2CwxX" src="/boom/client/f0605f03fa478593f75f791e8eea8889.svg" data-testid="heartFilled" alt="Favorited"></div></span></button>
Unclicked Button:
<button type="button" data-testid="favorite-button" class="styles__button--1wSNn styles__neutral--17MuV styles__elevation--2fhDh styles__elevationMedium--2eus4 styles__circle--3zgIv styles__small--127Kw"><span class="styles__iconAfter--3xNI0"><div class="FavoriteIcon__icon--2fuH8 FavoriteIcon__small--2hXns"><img class="styles__image--2CwxX" src="/boom/client/fe5b59d42e7d54796992f8f9914d3e45.svg" data-testid="heartOutline" alt="Favorite"></div></span></button>
How can I make it click only on the unclicked buttons?
I've already tried that:
driver.find_element_by_xpath("//input[#type="image"][#src="/boom/client/fe5b59d42e7d54796992f8f9914d3e45.svg"]).click()
But it doesn't seem to work.
Thanks.
To Click unclicked button Use the below Xpath to click.
driver.find_element_by_xpath("//button[.//img[data-testid='heartOutline']]").click()
To avoid synronization Issue Use WebDriverWait() and wait for element_to_be_clickable()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[.//img[data-testid='heartOutline']]"))).click()
you need to import below libraries.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Background:
I have the following HTML code that I am trying to get a XPATH to use with Selenium
<div class="btn-group">
<a type="button" class="btn btn-primary btn-user" onclick="AiD('182030801')" href="/download.pdf?id=182030801&h=917901e6659ad5eb53970aecf687b53e&u=cache&ext=pdf" target="_blank" style="border-top-left-radius: 3px;border-bottom-left-radius: 3px;">
<i class="fas fa-cloud-download-alt" aria-hidden="true" style="margin-right: 9px;margin-left: 2px;font-size: 25px;vertical-align: middle;color: #119802;"></i>Download ( PDF )
</a>
[...]
</div>
Code:
What have I tried to do with Python's Selenium is the following, however I cannot quiet get it to work without Python throwing an error:
browser.find_element(By.XPATH, "//div[#class='btn-group']/a").click()
browser.find_element(By.XPATH, "//div[#class='btn tn-primary btn-user']").click()
Error:
When I run the above snippet of code, the following error is produced and script crashes thereafter:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='btn tn-primary btn-user']"}
Problem:
The error states "no such element", however the element clearly exists "class="btn btn-primary btn-user""
Question:
How can I use Seleniums XPATH to "see" class="btn btn-primary btn-user" and click it to download a PDF?
Link:
PDFDrive
USe Xpath //div/a[#class='btn btn-primary btn-user']
Make sure the intended element is not under an iFrame. If it is then first you need to switch into that iFrame and then have to perform the action
Make sure you are using proper synchrnization and your element is ready. Introduce explicit wait as below:
driver.get('https://www.pdfdrive.com/querying-xml-xquery-xpath-and-sqlxml-in-context-d38665640.html')
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='btn-group']/a"))).click()
time.sleep(10) // it doesn't recommanded to put hardcoded wait but for debugging purpose you can check
Import below packages for this:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
You are using "div" tag but I can see respective class = btn btn-primary btn-user is inside "a" tag
try below code :
browser.find_element(By.XPATH, "//a[#class='btn tn-primary btn-user']").click()
Or you can go with another locator as well.
CSS : a.btn.btn-primarybtn-user
So you need to wait for the element to be clickable. Triggered the download.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = Options()
driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)
url = 'https://www.pdfdrive.com/querying-xml-xquery-xpath-and-sqlxml-in-context-d38665640.html'
driver.get(url)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'a.btn.btn-primary.btn-user'))).click()
I have the following HTML markup:
<div data-request-type="person" class="_entry _line _e _selected" id="c1055e27-0cfe-4f93-8bea-28a3421d842e" data-rolename="Member" data-isoptional="true">
<div class="_subindicator _gray"> </div>
<div class="_removePers"> </div>
<div class="_voluntaryPers"> </div>
<div class="_text">Member</div>
</div>
I have to click on the first <div> using .click().
But now I don't know how to find this with selenium. I already tried it with XPATH, but I have several elements with different IDs on this page. And the IDs are always regenerated. This does not work.
Does anyone have an idea?
I tried it with a lot of solutions...but nothing works.
My last one - to take a inner div with class _text
getAllMembers = browser.find_element_by_css_selector('td._text').get_attribute('innerHTML')
Please help - how can I do this with Selenium? :-)
Try below xpath :
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[#class='_entry _line _e _selected'][#data-rolename='Member']"))).click()
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
To select and click on a element by its attribute, you can use:
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)
driver.get("https://site.tld")
xpath = "//div[#data-rolename='Member']"
el = wait.until(EC.element_to_be_clickable((By.XPATH, xpath)))
el.click()
I have a problem with click in Selenium, it doesn't click on the button. This is my code:
from selenium import webdriver
import time
import click
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://help.instagram.com/contact/723586364339719/")
submit_button = driver.find_element_by_xpath('//*[#id="u_0_8"]')
submit_button.click()
This is the HTML code:
<button value="1" class="_42ft _4jy0 _4jy4 _4jy1 selected _51sy" type="submit" id="u_0_8">Enviar</button>
You can do it with using explicit wait for the button:
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, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[#id='u_0_8']"))).click()
I hope it helps you!
Instead of this:
submit_button = driver.find_element_by_xpath('//*[#id="u_0_8"]')
submit_button.click()
Try this:
driver.find_element_by_id("id='u_0_8']").click()
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:
driver.get('https://help.instagram.com/contact/723586364339719/')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()
Using XPATH:
driver.get('https://help.instagram.com/contact/723586364339719/')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[#value='1' and normalize-space()='Send']"))).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
Browser Snapshot: