0

I am writing a script to test a web portal. However, I need to change all protocols to SMB Server for me to conduct my test. I need to make it such that my script will only click on the button to change to SMB Server only when it is present as when it is already in SMB Server mode, the button does not exist.

Right now, I have only coded out the part to click on change to SMB Server mode.

This is my code to click to change to SMB server mode:

   driver.find_element_by_id("smb_server").click()
    alert=driver.switch_to_alert()
    alert.accept()

This is the html after it has changed to SMB server mode:

 <span class="table-head">
    Current Protocol
    </span>
Dan
  • 51
  • 1
  • 8
  • Refer [This Link](https://stackoverflow.com/questions/14156656/how-to-verify-element-present-or-visible-in-selenium-2-selenium-webdriver) for more info – Ali Jan 29 '19 at 10:04

3 Answers3

2

You can use findElements instead of findElement. This will return an empty list of no match is found, so that an exception can be avoided.

Boolean isAvailable = driver.findElements(By.yourLocator).size() > 0

This is with an assumption, that the button will be present/absent and not enabled/disabled.

Anitha.R
  • 344
  • 2
  • 15
  • I'm using Python Selenium to code the script and there seems to be a red underline under Boolean. I'm new to Python and Selenium and wondering how should I solve it – Dan Jan 30 '19 at 03:19
  • Sorry for the late reply. 1. There is no boolean in Python. 2. In python, you need to use find elements as below. `driver.find_elements(By.XPATH, "//a")` – Anitha.R Jan 31 '19 at 07:48
  • `if not elements:` //List empty, meaning no element is present – Anitha.R Jan 31 '19 at 07:58
0

You can test to see if the element exists and then click it if it does

button = driver.find_elements(By.ID, "smb_server")
if button
    button[0].click()
JeffC
  • 22,180
  • 5
  • 32
  • 55
-1

try below (in java pls change to corresponding language):

Boolean isPresent = driver.findElements(By.id("your id")).size() > 0;

  if(isPresent){
    <click the button>
  }
  • How do I change this to Python language? I'm sorry I'm new to this and I can't seem to initialize Boolean isPresent – Dan Jan 30 '19 at 03:21