0

Hi, I'm learning programming since yesterday. How I can tell my browser to click this?

input type="button" value="Chiudi" class="menu_select" onclick="if (this.form.do_not_show.checked) resetWarningProAlert('fidelity'); window.parent.dialog_windows['#dialog-warning_pro'].dialog('close');"

or this

span class="ui-icon ui-icon-closethick" unselectable="on" style="user-select: none;">close</span

Here there is a short part of my working code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox(executable_path="./drivers/geckodriver")
browser.get('https://www.example.it')
browser.find_element_by_name('example1')
digita1 = browser.find_element_by_name('example1')
digita1.send_keys('example2')
browser.find_element_by_name('example3')
digita1 = browser.find_element_by_name('example3')
digita1.send_keys('example4')
browser.find_element_by_name('example1')
digita1 = browser.find_element_by_name('example1')
digita1.click()
digita1.send_keys(Keys.ENTER)
Paul Lemarchand
  • 2,068
  • 1
  • 15
  • 27

2 Answers2

0

try this css_selector :

find_element_by_css_selector("input[type='button'][value='Chiudi']").click()
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Hello, thanks for your answer. I tried this: –  May 27 '21 at 20:16
  • elem = browser.find_element_by_css_selector("input[type='button'][value='Chiudi']") elem.click() –  May 27 '21 at 20:16
  • But the terminal always gives me this kind of problem: –  May 27 '21 at 20:17
  • Traceback (most recent call last): File "/home/ciro/python-selenium-basic/bigciao.py", line 19, in elem = browser.find_element_by_css_selector("input[type='button'][value='Chiudi']") –  May 27 '21 at 20:17
  • File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webdriver.py", line 598, in find_element_by_css_selector return self.find_element(by=By.CSS_SELECTOR, value=css_selector) –  May 27 '21 at 20:18
  • File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element return self.execute(Command.FIND_ELEMENT, { File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute self.error_handler.check_response(response) File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: –  May 27 '21 at 20:18
  • Message: Unable to locate element: input[type='button'][value='Chiudi'] –  May 27 '21 at 20:18
  • Can you check if there is any iframe involved ? – cruisepandey May 27 '21 at 20:39
  • Yes, there is a third party website that provides an iframe embed code to insert certain elements on my website –  May 27 '21 at 20:42
  • I think now I'm able to try to solve the problem –  May 27 '21 at 20:59
0

Like this!

driver.find_element_by_class("ui-icon-closethick").click()
driver.find_element_by_class("menu_select").click()

But this is assuming you only have one element that fits the description (or rather that the one you want is the first on the page)

With xpath (learn to use this, it's great and easy once you get it!)

driver.find_element_by_xpath("//span[contains(text(),'close')]").click()
driver.find_element_by_xpath("//input[@value='Chiudi']").click()

or just by appending .click() to the working find_element function.

Here's an easy way to get the xpath: Is there a way to get the XPath in Google Chrome?

yes
  • 177
  • 9
  • Where is `next-button` ? – cruisepandey May 27 '21 at 19:44
  • Sorry, I misread the question. Updated my answer. – yes May 27 '21 at 19:56
  • Hello, thanks for your answer. I tried in different ways, but linux terminal tells me this: –  May 27 '21 at 20:10
  • File "/home/ciro/python-selenium-basic/bigciao.py", line 19, in elem = browser.find_element_by_xpath("//input[@value='Chiudi'") File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath –  May 27 '21 at 20:11
  • return self.find_element(by=By.XPATH, value=xpath) File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element return self.execute(Command.FIND_ELEMENT, { File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute –  May 27 '21 at 20:11
  • self.error_handler.check_response(response) File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidSelectorException: Message: Given xpath expression "//input[@value='Chiudi'" is invalid: SyntaxError: The expression is not a legal expression. –  May 27 '21 at 20:11
  • Sorry, it was missing a ] to terminate the [@value=''] part. I updated the code, try again! – yes May 27 '21 at 20:12