0
def status_button_check():
    if(driver.find_element_by_xpath("//div[@role='button' and @title='Status']")):
        s_b_c_status = "True"
    else:
        s_b_c_status = "False"
    print(s_b_c_status)
status_button_check()

trying to check element if is there or not, but it gave me following error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: "method":"xpath","selector":"//div[@role='button' and @title='Status']"}

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Rabe
  • 67
  • 1
  • 2
  • 7
  • @ Andersson Irrespective of the element **found** or **not found** OP is trying to get a status. Doesn't looks the target duplicate is a justified one. Please have a re-look. – undetected Selenium Aug 31 '18 at 12:25
  • @DebanjanB , The question is *"Checking whether XPath (I guess it's "Element"/"WebElement", but not "XPath") is there or not"*... Nothing about *"getting status"*. Exact duplicate as for me – Andersson Aug 31 '18 at 12:35
  • @Andersson Of-coarse as a _tag moderators_ we need to need to help the OP editing the questions at-least from _New contributors_ so that the question reveals the actual issue OP is facing. Possibly instead of _Closing_ an _edit_ was expected. – undetected Selenium Aug 31 '18 at 12:42
  • 1
    @DebanjanB OP is not trying to get a status, they are trying to determine existance of an element related to status on the page. Your answer doesn't indicate that you thought any different and this question has been asked and answered many times. – JeffC Aug 31 '18 at 13:04
  • @Andersson I still don't think OP's question is about `element is there or not`, rather it's about getting the status about the presence of element (without any wait) which OP wants to print. The _edited heading_ completely changes the coarse of the question and OP won't receive any working answer either. – undetected Selenium Aug 31 '18 at 13:10
  • @DebanjanB , it doesn't matter what OP wants to do *after*: print some string or launch a rocket into space. The actual problem is getting exception in case *element is not there*. Both your and Rajagopalans' answers are just a modification of [this solution](https://stackoverflow.com/questions/45695874/check-if-element-exists-python-selenium/45696431#45696431) – Andersson Aug 31 '18 at 13:16
  • @DebanjanB If you actually believe that, why is your answer returning true if the element is there and false if it is not? – JeffC Aug 31 '18 at 13:19
  • 2
    this was correctly closed as a dupe... being a new contributor does not change the fact it has already been answered. – Corey Goldberg Aug 31 '18 at 14:56
  • 1
    @DebanjanB Where in the "Be nice" or Code of Conduct does it state that duplicate questions shouldn't be marked as a duplicate, new user or not? Please point us to that line. – JeffC Aug 31 '18 at 15:20
  • @DebanjanB and all, I have a [question](https://stackoverflow.com/questions/52118767/program-ended-without-completing-the-task) which is related to this... – Rabe Aug 31 '18 at 15:39
  • 2
    @Andersson I agree with you completely, I actually answered the question even before you closed the conversation. – Rajagopalan Aug 31 '18 at 19:45
  • @JeffC He wouldn't answer any of your meaningful question. – Rajagopalan Aug 31 '18 at 19:45
  • 2
    @DebanjanB I am not sure as to why you are always fighting with all the people in the stack overflow, can you please try to be nice with people here? – Rajagopalan Aug 31 '18 at 19:50

2 Answers2

3

Error message...

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: "method":"xpath","selector":"//div[@role='button' and @title='Status']"}

...implies that the XPath you have used was unable to locate any element within the HTML DOM.

If you want to validate the presence of the desired element you need to induce a try-catch{} block as follows:

def status_button_check():
    try:
        if(driver.find_element_by_xpath("//div[@role='button' and @title='Status']")):
            s_b_c_status = "True"
    except NoSuchElementException:
            s_b_c_status = "False"
    print(s_b_c_status)
status_button_check()
Chirag Jain
  • 1,367
  • 1
  • 11
  • 27
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I have a doubt which is 80% related to this question. Take a look at the [codes here](https://pastebin.com/dM2ztmmA), here it doesn't find `@title="Status"` then also it prints `Scanning done`. – Rabe Aug 31 '18 at 12:59
  • @Rabe , As you have defined `s_b_c_status = "False"` your `while` loop will not even start – Andersson Aug 31 '18 at 13:10
  • As per your code **Scanning Done** will always get printed as `print("Scanning Done!")` is out of the _try-catch{}_ block. – undetected Selenium Aug 31 '18 at 13:13
  • @DebanjanB What code are you looking at? There is no `print("Scanning Done!")` code in this answer or in the original question. There is a `print()` outside the `try-catch()` but it's there on purpose because OP wants to print that message no matter the status value. So, the code here is correct. – JeffC Aug 31 '18 at 16:52
0

Write this code

def status_button_check():
    if(driver.find_elements_by_xpath("//div[@role='button' and @title='Status']")>0):
        s_b_c_status = "True"
    else:
        s_b_c_status = "False"
    print(s_b_c_status)
status_button_check()
Rajagopalan
  • 5,465
  • 2
  • 11
  • 29
  • To be more efficient, the whole body could be `return driver.find_elements_by_xpath("//div[@role='button' and @title='Status']")>0`. You don't need the `if then`. – JeffC Aug 31 '18 at 13:20
  • @Rajagopalan , some remarks regarding your solution: 1) in Python there is no need in parenthesis (`if(condition)`), so `if condition` is OK, 2) You cannot compare *list* with *int*, so `if driver.find_elements(...) > 0` will lead to `TypeError`. You need to use either `if len(driver.find_elements(...)) > 0` or (preferable way) just `if driver.find_elements(...)`. Note that empty list always returns `False` while not empty list returns `True` – Andersson Aug 31 '18 at 21:42
  • @Andersson Thank you very much, I will keep that in my mind. – Rajagopalan Sep 01 '18 at 03:44