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")))
Related
I'm trying to search element by text with selenium but didn't find anything that work. The element is displayed below:
<div class="slick-cell l0 r0 project"><span class="project-name">MFIT</span></div>
There are multiple elements of this type so I need to find this element by MFIT and return the href.
I've tried multiples possibilities using XPath which didn't work.
driver.find_elements(By.XPATH, "//*[contains(#*, 'MFIT')]") and go over the list
driver.find_element(By.XPATH, "//*[contains(#*, 'MFIT')]")
driver.find_element(By.XPATH, "//span[#class='project-name' and contains(., 'MFIT')]")
Can someone help me ?
Try:
driver.find_element(By.XPATH, "//a[contains(text(), 'MFIT')]").get_attribute('href')
WebDriverWait(driver,20).untill(EC.visibility_of_element_located((By.XPATH, "//a[contains(text(), 'MFIT')]")))
#imports
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I'm using selenium to automate some tasks with webdriver.
Turns out I can't find a div to click on, selenium just can't find it.
Does anyone have a suggestion?
HTML :
<div aria-controls="leftAdvPnl_body" aria-expanded="false" aria-haspopup="true" class="rich-stglpanel-header " id="leftAdvPnl_header" onclick="SimpleTogglePanelManager.toggleOnClient(event,'leftAdvPnl');" onkeypress="return keypressclickhandle(event);" role="link" tabindex="0"><div aria-hidden="true" class="rich-stglpanel-marker"><div class="rich-stglpnl-marker" aria-hidden="true" id="leftAdvPnl_switch_on" style="display: none">«</div><div class="rich-stglpnl-marker" aria-hidden="true" id="leftAdvPnl_switch_off">»</div></div><span id="leftAdvPnl_header_label">Pesquisar</span><span aria-hidden="true"> </span></div>
Code phyton:
while len(navegador.find_elements_by_xpath('//*[#id="leftAdvPnl_header"]')) < 1:
time.sleep(1)
print("Procurando formulário do processo")
link=navegador.find_element_by_xpath("//*[#id="leftAdvPnl_header"]")
link.click()
Thanks!
You have 2 nested double quotes, instead of "//*[#id="leftAdvPnl_header"]" use '//*[#id="leftAdvPnl_header"]'
You need to wait for an element to be clickable, so try:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="leftAdvPnl_header"]'))).click()
To click on the element with text as Pesquisar you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "div.rich-stglpanel-header#leftAdvPnl_header span#leftAdvPnl_header_label").click()
Using xpath:
driver.find_element(By.XPATH, "//span[text()='Pesquisar' and #id='leftAdvPnl_header_label']").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:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.rich-stglpanel-header#leftAdvPnl_header span#leftAdvPnl_header_label"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Pesquisar' and #id='leftAdvPnl_header_label']"))).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
Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
xpath that you should check :
//div[#id='leftAdvPnl_header']
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.
If we have 1/1 matching node, Please make sure that :
This div is not under an iframe.
This div is not under a shadow-root.
You should not be on new tab/windows launched by selenium.
Code :
try:
WebDriverWait(navegador, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='leftAdvPnl_header']"))).click()
print("Clicked on web element")
except:
pass
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Updated :
CSS are far more better locator than XPath, please try the below one :
div[onclick^='SimpleTogglePanelManager.toggleOnClient'][onclick*='(event,'][onclick*='leftAdvPnl']
Same way to check if we have 1/1 matching node or not.
To click, there are 4 ways in Selenium.
Code trial 1:
time.sleep(5)
driver.find_element_by_css_selector("div[onclick^='SimpleTogglePanelManager.toggleOnClient'][onclick*='(event,'][onclick*='leftAdvPnl']").click()
Code trial 2:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[onclick^='SimpleTogglePanelManager.toggleOnClient'][onclick*='(event,'][onclick*='leftAdvPnl']"))).click()
Code trial 3:
time.sleep(5)
button = driver.find_element_by_css_selector("div[onclick^='SimpleTogglePanelManager.toggleOnClient'][onclick*='(event,'][onclick*='leftAdvPnl']")
driver.execute_script("arguments[0].click();", button)
Code trial 4 :
time.sleep(5)
button = driver.find_element_by_css_selector("div[onclick^='SimpleTogglePanelManager.toggleOnClient'][onclick*='(event,'][onclick*='leftAdvPnl']")
ActionChains(driver).move_to_element(button).click().perform()
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
my HTML-Code is following:
<button aria-label="Nur zuhören" aria-disabled="false" class=
"jumbo--Z12Rgj4 buttonWrapper--x8uow audioBtn--1H6rCK">
and I want to click on the button with following Python-Code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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
wait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "jumbo--Z12Rgj4 buttonWrapper--x8uow audioBtn--1H6rCK"))).click
but it's not working
If you want to find a element with multiple class name, you can use XPATH.
I simplify you html as <button class="aaa bbb ccc">
Then you can use (By.XPATH, "//button[contains(#class, 'aaa') and contains(#class, 'bbb') and contains(#class, 'ccc')]") to find this button.
Try to capture the element in webelement and then try to execute the expected condition command. It should work
click_ele=driver.find_element_by_class_name(jumbo--Z12Rgj4 buttonWrapper--x8uow audioBtn--1H6rCK)
wait(driver, 20).until(EC.element_to_be_clickable(click_ele.click)))
wait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "jumbo--Z12Rgj4 buttonWrapper--x8uow audioBtn--1H6rCK"))).click
.click might not be coming as an option in the dropdown, as it take 2 positional arguments
Selenium can't find multiple class names by class name instead use css selector.
Try below css selector.
wait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".jumbo--Z12Rgj4.buttonWrapper--x8uow.audioBtn--1H6rCK"))).click()
OR
wait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Nur zuhören']"))).click()
Hope this helps.
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()
while learning how to use selenium, Im trying to click an element but nothing happens and Im unable to reach the next page. this is the relevant page: http://buyme.co.il and Im trying to click: הרשמה
I managed to print the desired element (הרשמה) so I guess Im reaching the correct place in the page. but 'click()' doesnt work.
the second span <span>הרשמה</span> is what i want to click:
<li data-ember-action="636">
<a>
<span class="seperator-link">כניסה</span>
<span>הרשמה</span>
</a>
</li>
for elem in driver.find_elements_by_xpath('//* [#id="ember591"]/div/ul[1]/li[3]/a/span[2]'):
print (elem.text)
elem.click()
also tried this:
driver.find_element_by_xpath('//*[#id="ember591"]/div/ul[1]/li[3]/a').click()
I expected to get to the "lightbox" which contain the registration fields.
Any thoughts on the best way to accomplish this?
Explicit Waits - An explicit wait is a code you define to wait for a certain condition to occur before proceeding further in the code.
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()
browser.get("https://buyme.co.il/")
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, 'ember591')))
elm = browser.find_elements_by_xpath('//div[#id="ember591"]/div/ul[1]/li[3]/a')
elm[0].click()
Update:
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, 'login')))
email = browser.find_elements_by_xpath("//form[#id='ember1005']/div[1]/label/input")
email[0].send_keys("abc#gmail.com")
password = browser.find_elements_by_xpath("//form[#id='ember1005']/div[2]/label/input")
password[0].send_keys("test1234567")
login = browser.find_elements_by_xpath('//form[#id="ember1005"]/button')
login[0].click()
The desired element is an Ember.js enabled element so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use the following Locator Strategy:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='הרשמה']"))).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