Clicking a button with a conditional Selenium - python

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.

Related

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

Why selenium can't find some text on page

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

How to use click() on more then one element with the same XPath?

In my project, I want to use click() on a list with buttons. They all have the same xpath. But when I run through the list I scraped with seleniums driver.find_elements_by_xpath(). I just click on the first one and then the for loop breaks.
Don't know if I approach it the wrong way.
Code:
driver.get(url)
try:
myElem = None
myElem = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#href ='javascript:;']")))
if myElem != None:
buttons = driver.find_elements_by_xpath("//a[#href ='javascript:;']").click()
for button in buttons:
button.click()
HTML:
<a class="open" data-path="/public/employees/767772/description.json" href="javascript:;">
<span class="icon-caret-right"></span> 続きを読む
</a>
You can use List in selenium
List<WebElement> LIST = driver.findElements(By.xpath(""));
LIST.get(0).click();
first you need to store all clicks in list then if you want click first click you can do that with LIST.get(0).click();`
This line is your problem:
buttons = driver.find_elements_by_xpath("//a[#href ='javascript:;']").click()
You're doing find_elements but because of the click() at the end you're clicking on the first item then returning the response from the click() into buttons. That means buttons is not a list or array. I don't think anything returns from a click() so when you try to iterate through, you have nothing to iterate on.
Your code is mostly right If you drop the .click() on that line you will get a list and then you can iterate through:
buttons = driver.find_elements_by_xpath("//a[#href ='javascript:;']")
for button in buttons:
button.click()
This pretty much is the solution provided by #JustinLambert - but i think he's provided an example in the a different language.
In Python you don't need to worry about declaring it as a List<T> - just assign it to a variable as you already have done.

Python with Selenium - selecting a button or text action

I am, poorly, trying to 1. Click a button or 2. Check for text and then execute an action. I think I am just not coding this correctly.
eg
if driver.find_element_by_class_name('classOne').click()
elif:
"No Item" in driver.find_element_by_class_name('classTwo').driver.get(self_base_url)
This seems pretty simplistic, and I'm sure I've done something horribly wrong. This 'should' work, but isn't?
If the driver can't find an element, it will raise NoSuchElementException.
In your case, you can do:
x = driver.find_elements_by_class_name('classOne')
if len(x) > 0:
# click the first one found
x[0].click()
else:
print('No item x was found.')
Notice that I changed it to find_elements_by_class_name from find_element_by_class_name. This finds all elements if there are any and returns a list.

How to use Selenium function back() and I loss my variable in Python?

I'm new to Python.
I met a BIG problem in Python!!
I visit a website and put about 200 options from a dropdownlist in a array.
I want to click every options in the array and click javascript button to submit.
Take something I want from that page and back to previous page click another option.
Do those actions about 200 times in a for loop.
Here is the code:
for option in arrName:
if count > 0:
option.click()
string = u'Something'
link2 = browser.find_element_by_link_text(string.encode('utf8'))
link2.click()
//"do something I want"
browser.back()
count = count +1
In this code, I don't want to use first option.
PROBLEM comes,after the program click the second option, click the link2, and browser.back(), it answer me:
` StaleElementReferenceException: Message: stale element reference: element
is not attached to the page document
that means the options in array disappear?
How should I use the options in the array when the browser.back() in for loop?
Thanks
Yes, this is happening because of DOM refresh. You cannot simply iterate over an array and click back and forth. Best option is to find the element in runtime and then click. Avoid option.click() instead, find the next element with find_element. If you are not sure how to accomplish that then please provide the html

Categories