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()
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
The following HTML code is as proceeds:
<div class="lower-text">
<div data-text="winScreen.yourCodeIs">Your Code Is:</div>
<div>GENERATEDCODE</div>
What I'm trying to access is the second div node (the randomly generated number) in the class "lower-text"
try:
element = WebDriverWait(browser, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'lower-text'))
)
finally:
found = browser.find_element_by_xpath("//a[#class='lower-text'][2]").text
print(found)
This is what I've tried with no success.
I am unsure as how to access the second div node. Any help would be appreciated.
Please try below solution ::
element = WebDriverWait(driver, delay).until(
EC.presence_of_element_located((By.XPATH, "//*[#data-text='winScreen.yournumberis']/following-sibling::div")))
print element.text
Also you need to add below imports
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
I'm trying to find elements which have changing XPaths with only one part staying the same:
//*[#id="foo"]div[2]/div[1]/time
//*[#id="bar"]div/div[2]/div[1]/time
//*[#id="bat"]div[1]div[2]/div[1]/time
I already tried using driver.find_element_by_xpath("//*[contains(text(), '/div[2]/div[1]/time')]") but this doesn't seem to work.
Here is some example HTML:
<div class="entry-container">
<div class="entry-head">
<h3> some text </h3>
<time class="timestamp" datetime="2020-01-23 08:04:32 UTC">
Today at 18:34
</time>
</div>
</div>
I want to get the text from the time element.
The following XPath expression:
//div/div//time
will give you all time elements anywhere having two div elements as ancestor. This works for the example paths you provided.
Try this x path locator:
.//div[#class]/time
To handle dynamic element induce WebDriverWait() and visibility_of_element_located() and following xpath options.
XPath1 :
print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//h3[contains(.,'some text')]/following-sibling::time[1]"))).text)
XPath2 :
print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//h3[contains(.,'some text')]/following::time[1]"))).text)
XPath3 :
print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//div[#class='entry-head']//h3[contains(.,'some text')]/following-sibling::time[1]"))).text)
You need to add following libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
try with below code using Xpath and CSS with webdriver wait so element will be loaded in DOM
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Xpath -1
element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "(//time[#class='timestamp'])[1]")))
OR
Xpath -2
element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "(//time)[1]")))
By CSS
element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.CSS_SELECTOR , "time.timestamp")))
<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()
I've got a problem while trying to select a div tag class that has some white spaces in it.
This is the structure of page:
<div class="sadasd-dashboardtab even asdasd-syndicating_from_my_file" id="124121_1540012412412414">
<div id="124121_154006585856856858">
<span class="label">Syndicating From My File</span>
<button class="column-dropdown" title="Click for more tab options"></button>
</div>
</div>
This are my tries of code for this part:
#syndicating_button = driver.find_element_by_xpath("//span[text()='Syndicating From My File']")
#syndicating_button = driver.find_element_by_xpath("//div[#class='yui3-dashboardtab even s-tab-syndicating_from_my_site']")
syndicating_button = driver.find_element_by_css_selector("div.yui3-dashboardtab.even.s-tab-syndicating_from_my_site")
syndicating_button.click()
Your issue has nothing to do with "blanks"/"whitespaces" as your selectors should work well... if element is present in DOM. Try to wait until element appears in DOM and becomes clickable:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Syndicating From My Site']"))).click()