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
Related
I want to click on an element which contains class and title in selenium python.
A webpage contains repeatable class without any id but with unique name.
I want to detect and click on this title 'PaymateSolutions' once its loads in the page.
Below is the html tag. I tried many ways but I am ending up with errors.
Fyi I cant use the find element by class as they are not unique.
<div class="MuiGrid-root MuiGrid-item" title="PaymateSolutions">
<p class="MuiTypography-root jss5152 MuiTypography-body1">PaymateSolutions</p>
</div>
Few approaches that i tried to get driver element based on title using XPATH
Approach 1:-
wait = WebDriverWait(driver, 20)
element = wait.until(EC.element_to_be_clickable((By.XPATH,"//class[#title='PaymateSolutions']")))
Approach 2:-
element2 = (WebDriverWait(driver, 30).until(
EC.visibility_of_element_located((By.XPATH, "//p[#title='PaymateSolutions']")))
)
Approach 3:-
element2 = (WebDriverWait(driver, 30).until(
EC.visibility_of_element_located((By.XPATH, "//[#title='PaymateSolutions']")))
)
Can someone please help here?
For Approach 1 - title is the attribute of div tag. So the Xpath would be something like below:
//div[#title='PaymateSolutions']
For Approach 2 - p tag has no title attribute. PaymateSolutions is the text of the p tag. Xpath should be something like this:
//p[text()='PaymateSolutions']
For Approach 3 - There is no Tag Name in the xpath. Xpath would be:
//*[#title='PaymateSolutions']
Or
//div[#title='PaymateSolutions']
Links to refer - Link1, Link2
We can apply Explicit waits like below:
# Imports required for Explicit waits:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver.get(url)
wait = WebDriverWait(driver,30)
payment_option = wait.until(EC.element_to_be_clickable((By.XPATH,"xpath for PaymateSolutions option")))
payment_option.click()
Link to refer for the Explicit waits - Link
All the XPath that you've been trying seems a bit wrong. Please use the below XPath :
//div[#title='PaymateSolutions']//p[text()='PaymateSolutions']
Code trial 1:
time.sleep(5)
driver.find_element_by_xpath("//div[#title='PaymateSolutions']//p[text()='PaymateSolutions']").click()
Code trial 2:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#title='PaymateSolutions']//p[text()='PaymateSolutions']"))).click()
Code trial 3:
time.sleep(5)
button = driver.find_element_by_xpath("//div[#title='PaymateSolutions']//p[text()='PaymateSolutions']")
driver.execute_script("arguments[0].click();", button)
Code trial 4:
time.sleep(5)
button = driver.find_element_by_xpath("//div[#title='PaymateSolutions']//p[text()='PaymateSolutions']")
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
I have a span class attribute which I need to click and pass value to it.
Below is my span class:
<span class="input-group-addon-transparent icon-search sysparm-search-icon"></span>
Please do help me out. Thanks in Advance.
Without knowing the full HTML of the site you can get the first span which matches those classes by using find_element_by_css_selector:
selector = "span.input-group-addon-transparent.icon-search.sysparm-search-icon"
element = driver.find_element_by_css_selector(selector)
element.click()
element.sendKeys("value")
Or waiting for clickable state:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
selector = "span.input-group-addon-transparent.icon-search.sysparm-search-icon"
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, selector)))
element.sendKeys("value")
Where driver is your Selenium webdriver object
You case use class name to get that webelement.
webele= driver.find_element_by_class_name("input-group-addon-transparent icon-search sysparm-search-icon")
webele.click();
webele.sendkeys("any string/number")
or
driver.find_element_by_xpath("//*[contains(#class,'search-icon')]
//and then rest of the code
Use web driver wait before clicking on button
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "XPATH"))
Find the xpath of the span using any browser console then try using this code below to click and pass the value to it
span = find_element_by_xpath('''the x_path of the class''').click()
span.sendKeys('''value to be passed''')
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")))
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