Why selenium can't find some text on page - python

I have some page and "tree menu". When I want to open tree_menu, I need to click on button. After, I make some steps and click on title, for example, I clicked "19,20", after I clicked "may" and after I clicked "2" and in the top of page (where i choose this tree_menu) I have a button/link. And text in this element with 2 space (not one):
"19,20 > may > 2"
But on display I think I see with 1 space, but in tag value with 2. And this ">" is text, but I don't know how it understands and sees selenium.
So, after I call my function for search this text:
def isText(self, text):
try:
element = self.driver.find_element(By.XPATH, "//*[text()[contains(., \"" + text + "\")]]")
return True
except:
return False
So, but selenium can't to find this text. And it could find only for "19,20", but space or next text - no.
I opened web-code, but I didn't see new tag or something else.
But I tried find this button/link like element and check that text and it worked.
This project work with dojo, but i can't to change anything.
Please, can you help me with this problem?
TY!
Have a good day!

I didn't get exactly what your intension but if you want to find the button and click using selenium then follow the format.
element = self.driver.find_element(By.XPATH," COPY FULL XPATH").clik()
or
element = self.driver.find_element(By.XPATH," COPY FULL XPATH")
time.sleep(5)
element.click()

Just find the tag that contains that text.
element = self.driver.find_element(By.XPATH, "//*[contains(text(), '{}')]".format(text))

Related

Clicking a button with a conditional Selenium

I'm a beginner, learning selenium packpage using python language.
How could I click on the button with the text: "Co-assinar" only when there is the text: 'Segue a certidão..."
I'll put an image below the part of the code I'm talking about.
https://i.stack.imgur.com/8jV64.png
Your best bet is to "collect" the two elements you'd like as variables and then use an if statement to check and see if the elements text is equal to "Segue a..." and in that case click.
Something like this:
#Get the button you want to click
button_to_click = driver.find_element(By.CLASS_NAME, 'menor link_amissao_assinar...')
#Get the element that holds the text you're checking
text_you_want = driver.find_element(By.CLASS_NAME, 'texto_original').children[2].text
#If element text equals 'Segue a...' click
if text_you_want == 'Segue a...':
button_to_click.click()
try:
driver.find_element(By.XPATH,"//p[contains(text(),'Segue a certidão')]")
driver.find_element(By.XPATH,"//a[./i[text()=' Co assinar ']]").click()
except:
pass
You can check for the second element and then click the first element if it's there. Handling any exceptions you may want to in case.

(selenium)If I know only the innertext of the element button, how to click?

I'm new to selenium. And to python, too
If the button that includes the specific innerText exists, I wanna click it.
But I don't know any id or xpath. How can I find it and click?
That is, I have only the innerText, How can I find it, and click it?
txt = 'button_i_wanna_click'
if txt in driver.page_source:
try:
driver.find_element_by_id(# id_of_element_that_includes the txt).click()
except TIMEOUTexception:
"how can I do??"
You can use xpath here like -
driver.find_element_by_xpath("//button[contains(text(),'button_i_wanna_click')"])
Locate the element using xpath. For example
btn_txt = repr('Post Your Answer')
btn_xpath = f"//button[normalize-space(text())=${btn_txt}]"
btn = driver.find_element_by_xpath(btn_xpath)

Selenium cant find class to iterate and scrape text

as you can see in the first picture, my objective is to click and open every "Ver detalles" and to get all the text within it (which is shown in the third picture.
Here is the HTML for this first screen:
And here is the screen that opens once you click "ver detalles"
And its HTML
So far this I have made up some lines of code but I know it is useless because I am looking by XPATH and not by Class (and this will only return data for one), but whenever I look by class and try to iterate it doesn't find the class.
Please let me know if I made myself clear. Thanks beforehand
EDIT:
Thanks #cruisepandey it helped me open "ver detalles". Now I'm stucked trying to get the text out of it and click de X to close it and move on to the next "ver detalles".
This is the code I have so far, i have tried looking up by class, tag, etc but can't seem to find a way :(.
def order_data():
list_of_ver_detalles = driver.find_elements(By.XPATH, "//span[contains(text(),'Ver detalle')]/..")
sleep(3)
for ver in list_of_ver_detalles:
ver.click()
print(driver.find_element_by_class_name("jss672").text)
sleep(2)
driver.find_element(By.XPATH, "/html[1]/body[1]/div[6]/div[3]/div[1]/div[1]/div[1]/img[1]").click
Here is the text I am trying to print
And here is the X I am trying to click
I want to clarify few of your doubts when you say it is useless because I am looking by XPATH and not by Class - no it is not. for finding more than one element with any locator (assuming that locator in DOM can represent multiple entity) all you have to do is to use find_elements instead of find_element.
store all of Ver detalle like this :
list_of_ver_links = driver.find_elements(By.XPATH, "//span[contains(text(),'Ver detalle')]/..")
for ver in list_of_ver_links:
ver.click()
#Now write the code to fetch order details here

Selenium Webdriver failing to click. Unsure why

I have the following code;
if united_states_hidden is not None:
print("Country removed successfully")
time.sleep(10)
print("type(united_states_hidden) = ")
print(type(united_states_hidden))
print("united_states_hidden.text = " + united_states_hidden.text)
print("united_states_hidden.id = " + united_states_hidden.id)
print(united_states_hidden.is_displayed())
print(united_states_hidden.is_enabled())
united_states_hidden.click()
The outputs to the console are as follows:
Country removed successfully
type(united_states_hidden) =
<class 'selenium.webdriver.remote.webelement.WebElement'>
united_states_hidden.text = United States
united_states_hidden.id = ccea7858-6a0b-4aa8-afd5-72f75636fa44
True
True
As far as I am aware this should work as it is a clickable web element, however, no click is delivered to the element. Any help would be appreciated as I can't seem to find anything anywhere else. The element I am attempting to click is within a selector box.
Seems like a valid WebElement given you can print all of the info. like you did in your example.
It's possible the element located is not the element that is meant to be clicked, so perhaps the click is succeeding, but not really clicking anything.
You could try using a Javascript click and see if that helps:
driver.execute_script("arguments[0].click();", united_states_hidden)
If this does not work for you, we may need to see the HTML on the page and the locator strategy you are using to find united_states_hidden so that we can proceed.

Problem clicking on link in header in selenium (python)

Hello I have an issue clicking on the link "Assortiment"
on this page: https://www.colruyt.be/fr
In order to click it I use :
element = browser.find_element_by_xpath('.//li[#class = "first leaf menu-mlid-9143"]')
element.click()
There are two elements that can be found with that xpath. The first one is hidden in the side pullout menu for the mobile version and the second is the one you want. Try scoping your xpath to the div with main-navigation class.
browser.find_element_by_xpath('.//div[#class = "main-navigation"]//li[#class = "first leaf menu-mlid-9143"]')

Categories