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 -
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
<div data-offset-key="hch7-0-0" class="_1mf _1mj"><span data-offset-
key="hch7-0-0"><br data-
text="true"></span></div>
<span data-offset-key="hch7-0-0"><br data-text="true">
<span data-text="true"></span>
I want to enter data in this last span tag with data-text=true, how do I do that with selenium(python)?
My code:(want to post something on the wall of a group on facebook), I opened the post box successfully but I cant enter any input in that. That input is supposed to go in the last span tag in the HTML code above.
path = 'C:\Program Files (x86)\chromedriver.exe'
def fun(gmailID,password):
driver = webdriver.Chrome(path)
driver.get('https://www.facebook.com/')
user = driver.find_element_by_id('email')
user.send_keys(gmailID)
pass_enter = driver.find_element_by_id("pass")
time.sleep(1)
pass_enter.send_keys(password)
pass_enter.send_keys(Keys.RETURN)
time.sleep(5)
fb_link = 'https://www.facebook.com/groups/'
group_id = 'writersnbloggers'
driver.get(fb_link+group_id)
time.sleep(3)
message = driver.find_element_by_xpath("//*[contains(text(),
'Shivam?')]")
message.click()
time.sleep(2)
post_box = driver.find_element_by_xpath('//*span[#data-text="true"]')
print(post_box)
post_box.send_keys("hello")
time.sleep(10)
if __name__=="__main__":
fun(id,pwd)
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.facebook.com")
driver.find_element_by_xpath('//*[contains(text(),"What\'s on your mind")]').click()
WebDriverWait(driver, 10).until(EC.presence_of_element_located(
(By.CSS_SELECTOR, 'div[role="dialog"] form[method="POST"]')))
driver.switch_to.active_element.send_keys("something")
you can use switch to active element instead ,to focus on the current active element that is the create post
Try this python code:
WebDriver driver;
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.querySelector(\"div._1mj>span:last-child\").setAttribute(\"data-text\", \true\");");
Demonstration how it will work:
var lastSpan = document.querySelector("div._1mj>span:last-child");
console.log("Before : ");
console.log(lastSpan);
lastSpan.setAttribute("data-text", "true");
console.log("After : ");
console.log(lastSpan);
<div class="_1mf _1mj" data-offset-key="hch7-0-0">
<span data-offset-key="hch7-0-0"><br data-text="true"></span>
<span data-offset-key="hch7-0-0"><br data-text="true"></span>
<span ></span>
</div>
<div class="clearfix entry_but_bar_out clearfix" style="display: none;">
<div class="entry_but_bar_in clearfix">‹ yanıtlaeditsilmesajgammazla#208281235<i class="ssicon-facebook"></i>
<i class="ssicon-twitter"></i>
</div><!-- entry_but_bar_in clearfix -->
</div>
i'm trying to make elements visible and click on it . But it doesn't work ,
clicking = browser.find_element_by_xpath('//*[#id="middle-block"]/ol/li/div/div[3]/div[2]/span')
browser.execute_script("removeAttribute('display:none');",clicking)
clicking.click()
Please try the below code and let me know if it works for you.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ActionChains
import time
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
action = ActionChains(driver)
# Put your URL
driver.get('Your URL')
time.sleep(5)
Target_Div = driver.find_element_by_xpath('//div[contains(#class,"clearfix entry_but_bar_out clearfix")]')
driver.execute_script("arguments[0].setAttribute('style','display:\"true\"')", Target_Div)
Use the above code and make correction in your code.
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 am trying to get the snapshot URL from the snapshot window that opens when ALT + S is pressed. I can save the image. but instead i want to get the URL that is in the snapshot box and save it in a variable
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC1
from selenium.webdriver.common.by import By
driver = webdriver.Chrome('C:\\Windows\\chromedriver.exe') # change as per your location
driver.get ("https://www.tradingview.com/chart/1pPtW0th/")
driver.maximize_window()
ActionChains(driver).key_down(Keys.ALT).send_keys('s').perform()
wait_time = 25 # a very long wait time
#want to get snapshot URL here, instead of saving it.
element = WebDriverWait(driver, wait_time).until(EC1.element_to_be_clickable((By.LINK_TEXT, 'Save image')))[enter image description here][1]
time.sleep(3)
driver.close()
You could try targeting the following <div> element and extract the attribute "data-clipboard-text":
<div data-clipboard-text="https://www.tradingview.com/x/ODEmfXoU/" class="copyBtn-1oB8tEqo- shadow-2JTdO0Fb-">
<button class="button-2O-nMUcz- active-2UxWxOgk- withPadding-_5CJoO5q- ghost-3yO24wIn- primary-1rSzOFdX-" tabindex="0" title="" target="" href="">
<span class="hiddenText-3qcN5Wif-">Copy</span>
<span class="text-2KOWx3rB-">Copy</span>
</button>
</div>