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
Related
how to click a button in a webpage without class name or id,
this is the web code
<a href="https://example.com" style="text-decoration:none;line-height:100%;background:#5865f2;color:white;font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:15px;font-weight:normal;text-transform:none;margin:0px;" target="_blank">
Verify Email
</a>
I tried this code
driver.find_element_by_xpath('/html/body/div/div[2]/div/table/tbody/tr/td/div/table/tbody/tr[2]/td/table/tbody/tr/td/a').click()
but it show this in the log
Message: no such element: Unable to locate element:
and I tried this too
driver.find_element_by_link_text('Verify Email').click()
and it show this in the log
Message: no such element: Unable to locate element:
and I tried to add time.sleep(5) before it but the same problem
looks like there are spaces, so can you try with partial_link_text as well :-
driver.find_element_by_partial_link_text('Verify Email').click()
or with the below xpath :
//a[#href='https://example.com']
or
//a[contains(#href,'https://example.com')]
or
//a[contains(text(),'Verify Email')]
Update 1 :
There is an iframe involved, so driver focus needs to be switch to iframe first and then we can click on Verify Email, see below :-
Code :
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://www.gmailnator.com/sizetmp/messageid/#17b0678c05a9616e")
driver.execute_script("var scrollingElement = (document.scrollingElement || document.body);scrollingElement.scrollTop = scrollingElement.scrollHeight;")
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "message-body")))
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(),'Verify Email')]"))).click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Try this XPath locator:
//a[contains(text(),'Verify Email')]
Or this:
//a[#href='https://example.com']
So your code will be
driver.find_element_by_xpath("//a[contains(text(),'Verify Email')]").click()
or
driver.find_element_by_xpath("//a[#href='https://example.com']").click()
Also possibly you have to add a wait to let the element loaded.
In this case your code will be:
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, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, "//a[contains(text(),'Verify Email')]"))).click()
or
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, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, "//a[#href='https://example.com']"))).click()
I'm trying to find an element by xpath, but it does not return anything.
It seems all find_elements methods work except xpath.
I'm trying to find the text Ponderosa Campground which is clickable.
def searchCondition():
elem = driver.find_elements_by_xpath("//div[#aria-label='PONDEROSA CAMPGROUND']")
elem.click()
here is the URL where I'm trying to find the element.
https://www.recreation.gov/search?q=Ponderosa%20Campground
<div data-component="FocusManager" tabindex="-1" style="outline: none;">
<div class="flex-grid search-outer-wrap" data-component="FlexRow">
<div class="flex-col-12" data-component="FlexCol">
<div class="rec-flex-card-wrap " id="rec-card-233118_campground">
<a data-card="true" class="rec-flex-card-image-wrap" href="/camping/campgrounds/233118" target="_blank" rel="noopener noreferrer" alt="PONDEROSA CAMPGROUND">
<div data-component="FauxImage" class="sarsa-faux-image rec-flex-card-image-wrap-faux-image" role="img" aria-label="PONDEROSA CAMPGROUND" style="min-height: 155px; background-image: url("https://cdn.recreation.gov/public/images/76799.jpg");"></div></a>
<div class="rec-flex-card-content-wrap"><a href="/camping/campgrounds/233118" target="_blank" rel="noopener noreferrer" aria-label="PONDEROSA CAMPGROUND - 2.6 stars / $25 / night">
Here is the full script.
import time
from time import sleep
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
import pause
import pyperclip
# Definitions
ID = ""
PW = ""
SRCH = "Ponderosa Campground"
URL = "https://www.recreation.gov/search?q=Ponderosa%20Campground"
now = datetime.now()
options = Options()
options.headless = False
# executable_path for webdriver
driver = webdriver.Chrome(
executable_path='C:/chromedriver.exe',
options=options)
wait = WebDriverWait(driver, 50)
driver.get(URL)
def searchClick():
CampBtn = wait.until(EC.element_to_be_clickable((By.XPATH,"//a[#alt='PONDEROSA CAMPGROUND']"))).click()
elem = driver.find_element_by_xpath("//a[#alt='PONDEROSA CAMPGROUND']")
if elem.isDisplayed():
print(elem)
elem.click()
else:
print("no availability")
searchCondition()
time.sleep(20)
You have to point your xpath to the interactive element for example a, input, button
And have to induce some explicit wait with expected condition to check that the element is all set to perform click operation. For example -
WebDriverWait(web, 20).until(EC.element_to_be_clickable((By.XPATH,"//a[#alt='PONDEROSA CAMPGROUND']")))
elem = web.find_element_by_xpath("//a[#alt='PONDEROSA CAMPGROUND']")
elem.click()
After clicking on the image it opens up new tab as below. To see this operation add some wait for sometime after click operation -
Question.
I'm really new to Python and can't find a way to click this link.
My aim was to click the links one by one, and I got stuck from clicking the first link.
I searched several times and tried even more, but I can't even find what is the problem!
The links lead to a new window(Survey), and the following is the html structure.
<div id="bb_deployment6" class="stream_item active_stream_item" role="listitem" x-aria-selected="true" tabindex="0" style="padding-left: 20px;"><span class="stream_datestamp">1 hour</span><div class="stream_context">Survey [Today] Survey A: Click to submit survey </div><div class="stream_details"></div><div class="stream_context_bottom"></div></div>
<div id="bb_deployment5" class="stream_item" role="listitem" x-aria-selected="false" tabindex="-1" style="padding-left: 20px;"><span class="stream_datestamp">2 hour</span><div class="stream_context">Survey [Today] Survey B: Click to submit survey </div><div class="stream_details"></div><div class="stream_context_bottom"></div></div>
Here's what I've tried
First Shot
from selenium import webdriver
browser =webdriver.Chrome("C:\Pii\selenium\chromedriver.exe")
#Open the Site
browser.get("https://that site")
#Find & Click!!
browser.find_element_by_partial_link_text("Survey").click()
The first error code was
: Message: no such element: Unable to locate element: {"method":"partial link text","selector":"Survey"}
Second Shot: OK Maybe the loading time was too short?
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
browser =webdriver.Chrome("C:\Pii\selenium\chromedriver.exe")
browser.get("https://that site")
#Wait & Click
wait = WebDriverWait(browser, 10)
element = wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Survey")))
browser.find_element_by_partial_link_text("Survey").click()
and now it said
: selenium.common.exceptions.TimeoutException: Message:
Third Shot: Maybe the click part was the problem because of onclick?
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
browser=webdriver.Chrome("C:\Pii\selenium\chromedriver.exe")
browser.get("https://that site")
wait = WebDriverWait(browser, 10)
element = wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Survey")))
sample = browser.find_element_by_link_text("Survey")
browser.execute_script("arguments[0].click();",sample)
and it said
: selenium.common.exceptions.TimeoutException: Message:
The same message as above
Fourth Shot: Maybe I should use XPATH instead of text?
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
browser=webdriver.Chrome("C:\Pii\selenium\chromedriver.exe")
browser.get("https://that site")
wait = WebDriverWait(browser, 10)
element = wait.until(EC.presence_of_element_located((By.XPATH, '//*[#id="bb_deployment5"]/div[1]/a')))
sample = browser.find_element_by_xpath('//*[#id="bb_deployment5"]/div[1]/a')
browser.execute_script("arguments[0].click();",sample)
and the result was the same
I think I got something totally wrong, but I can't get what that is.
Any answer would be a great help. Thanks
Since the element is located in a different iframe, you should switch focus to that iframe and then search for the element. This is how you do it:
iframe = browser.find_element_by_class_name('cloud-iframe')
browser.switch_to.frame(iframe)
element = wait.until(EC.presence_of_element_located((By.XPATH, '//*[#id="bb_deployment5"]/div[1]/a')))
element.click()
This should work, but I am not 100% sure as I haven't yet seen the website myself.
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()
<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()