I have a html content like this
<div class="ng-star-inserted">
<span class="ng-star-inserted">
Seismic interpreter
</span>
<span class="ng-star-inserted">
Geophysicist
</span>
</div>
I have a selenium script where I am trying to get the first role name and second role name.
I tried this, but it's not giving me the 1st and 2nd elements as expected. Any idea what am I missing? thanks!
'//div[#class="ng-star-inserted"]//span//a[1]'
'//div[#class="ng-star-inserted"]//span//a[2]'
You can xpath indexing in this case :
so instead of this :
'//div[#class="ng-star-inserted"]//span//a[1]'
'//div[#class="ng-star-inserted"]//span//a[2]'
use this :
"(//div[#class='ng-star-inserted']//span//a)[1]"
"(//div[#class='ng-star-inserted']//span//a)[2]"
or if you think the text won't change, you can make use of LINK_TEXT as well or PARTIAL_LINK_TEXT
Sample code :-
wait = WebDriverWait(driver, 10)
button1 = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Seismic interpreter")))
button1.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
So you can use the below way
((//a[contains(text(),'Seismic interpreter')])[2])
((//a[contains(text(),'Geophysicist')])[2])
clickLink=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"((//a[contains(text(),'Seismic interpreter')])[2])")))
checkbox.click()
Related
Currently, I try to select a label by clicking on it but a problem the element has no ID:
<div class="cas__wayf-idp h5-like">
<span class="space space--sm"></span>
<input type="radio" name="selection" class="js-wayf-composant" id="idp-EDU" value="EDU" data-memorisable="true" checked="checked">
<label for="idp-EDU" class="form__label">TEXT IN THE LABEL</label>
</div>
I see that there are plenty of other ways including the xpath with text. After many tries I got this result:
driver.find_element("xpath", "//*[text()='TEXT IN THE LABEL']").click()
And this error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[text()='TEXT IN THE LABEL']' is not a valid XPath expression.
Hope you can help me, thanks.
You can use contains method to select text node
driver.find_element(By.XPATH, "//*[contains(text(),'TEXT IN THE LABEL')]").click()
#OR
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//*[contains(text(),"TEXT IN THE LABEL")]'))).click()
#Imports
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
I want to be able to select the button that has the timestamp i am targeting. Here is the HTML:
<a class="mdc-button mdc-button--unelevated mdc-ripple-upgraded" href="#" onclick="selectTime(7160, null, "2021-08-23T08:15:00-04:00", '8b96beed5b0b8f7b34e51853f14ef6eb'); return false;" style="--mdc-ripple-fg-size:55px; --mdc-ripple-fg-scale:1.97805; --mdc-ripple-fg-translate-start:17px, -19.5px; --mdc-ripple-fg-translate-end:18.5px, -9.5px;">
<div class="mdc-button__ripple"></div>
<span class="mdc-button__label">8:15 AM</span>
</a>
I do not know how to target the timestamp parameter it is calling in onclick that can be used to select the button. I have tried to find element by targeting the label's text, but I cannot click on the label, with it giving me this error :
Message: element click intercepted: Element <span class="mdc-button__label">...</span> is not clickable at point (132, 380). Other element would receive the click: <div class="mdc-button__ripple"></div>
Any help would be greatly appreciated.
You can use the below css_selector :
a[class$='mdc-ripple-upgraded'] span[class='mdc-button__label']
use it like this :
wait = WebDriverWait(driver, 10)
ActionChains(driver).move_to_element(wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[class$='mdc-ripple-upgraded'] span[class='mdc-button__label']")))).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 been trying to find a way to click a specific li element by using the text inside, however, whatever I try seems to not be finding the element.
The HTML is;
<ul class="shoes-sizen-mp" id="ul_top_hypers" style="overflow: auto;">
<li id="li_284" onclick="return select_size('284')"><a class="a_top_hypers"> 3.5 <span style="display: none">,</span><br> 35.5<br></a></li>
<li id="li_285" onclick="return select_size('285')"><a class="a_top_hypers"> 4 <span style="display: none">,</span><br> 36<br></a></li>
This is part of a list where you can select your size (such as 3.5,4,4.5,5). I want to be able to click the specific one by using the text such as 3.5, for example.
Edit
driver.find_element_by_xpath(f"//ul[#id='ul_top_hypers' and starts-with(#id, 'li_'][contains(text(),'{user_shoe_size}')]").click()
sleep(20)
Above is one of the many things I have tried to locate the element, but nothing just yet.
Any help would be greatly appreciated. Thanks a lot!
To click on dynamic induce WebDriverWait() and wait for element_to_be_clickable() and following xpath.
def click_size(size):
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//ul[#class='shoes-sizen-mp']//li[contains(.,'{}')]".format(size)))).click()
click_size("3.5")
You need to import below libraries.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
</div>
<fieldset class="pi-search-form__footer">
<div class="pi-search-form__footer-cta" data-ng-show="stay.isStandardBooking()">
<button class="btn btn--primary" data-ng-click="submitSearch()">
<span data-ng-switch="ctaText">
<span data-ng-switch-when="checkavailability">Check availability</span>
<span data-ng-switch-when="searchnow">Search now</span>
<span data-ng-switch-default data-pi-track-click="SEARCH_AGAIN">Search</span>
</span>
</button>
</div>
This is my button code and I want to click "Check availability" button.
I use that code but it not works.
browser.find_elements_by_xpath(".//span[contains(text(), 'Check availability')]")
Here is the Answer to your Question:
As you want to click on a button so your xpath should be unique and should identify a single element. Hence instead of find_elements_by_xpath you should consider using find_element_by_xpath. To click on the Check availability button you can use the following line of code:
//ensure the imports
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
//other code
//click on Check availability button
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn--primary']//span[#data-ng-switch-when='checkavailability']")))
browser.find_element_by_xpath("//button[#class='btn btn--primary']//span[#data-ng-switch-when='checkavailability']").click()
Let me know if this Answers your Question.
it should be
browser.find_elements_by_xpath(".//span[contains(text(), 'Check availability')]")
and if it's not working use explicit wait.
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 30)
checkAvailability = wait.until(EC.presence_of_element_located((By.XPATH, ".//span[contains(text(), 'Check availability')]")))
[Python 2.7, Selenium Web-driver]
So there's this Website:
<div class="flex-module-header">
<h3><span class="trend-location js-trend-location">Greece Trends</span></h3>
<span class="middot">ยท</span> <a role="button" href="#" data-modal="change-trends" class="change-trends js-trend-toggle">Change</a>
And I want to click the button:
<a role="button" href="#" data-modal="change-trends" class="change-trends js-trend-toggle">Change</a>
And I've tried everything (trying to locate by xpath, class etc.), and I can't seem to be able to do what I'm looking for.
I basically want to click the button, that's all.
As you didn't provide error stack trace and tried code, Assuming you didn't use WebDriverWait yet to wait until element visible and clickable. So I'm giving the example with this as below :-
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, 10)
link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Change")))
link.click()
Note :- Before using this, make sure element is not present inside a frame or iframe. If element is present inside a frame or iframe you need to switch that frame before finding the button as driver.switch_to_frame("frame name or id").
Hope it helps...:)