How do I get text that appear on page from div? - python

The page source look like this:
<div class="caption">
<div id="question_speaker" style="display: none;"><img
src="../server/static/loudspeaker.png"></div>
<div class="translations">explain</div>
<div> </div>
</div>
I'm trying to get "explain" to text variable in python.

CSS:
div.caption div#question_speaker+div
XPATH:
//div[#class='caption']//div[#id='question_speaker']//following-sibling::div
Before using them make sure that they are unique in HTML-DOM
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath/css and see, if your desired element is getting highlighted with 1/1 matching node.
Code:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='caption']//div[#id='question_speaker']//following-sibling::div"))).text)
You'll need below import:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Related

Selenium Pyhton: click on a label without ID (4.3.0)

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 am trying to click a button that is inside nested divs using Selenium

The structure looks like this:
<div class="edit-post-header">
<div class="edit-post-header__settings">
<button type="button" aria-disabled="false" class="components-button editor-post-publish-button editor-post-publish-button__button is-primary">Publish</button>
</div>
</div>
I am trying to click it like this:
browser.find_element_by_xpath("//div[#class='edit-post-header']//div[#class='edit-post-header__settings']//button[#class='editor-post-publish-button__button']").click()
But it does not work.
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.
xpath that you should check :
//button[text()='Publish']
if it is unique, please use below code to click :
Code trial 1 :
time.sleep(5)
driver.find_element_by_xpath("//button[text()='Publish']").click()
Code trial 2 :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Publish']"))).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

Not able to target 2nd matching element by XPATH

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()

Finding li element by text inside - Selenium Python

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

how to find button to click in Selenium using python?

</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')]")))

Categories

Resources