Home > Mobile >  Selenium, how to random select the item from a table and random select the items, click on it?
Selenium, how to random select the item from a table and random select the items, click on it?

Time:02-02

from selenium import webdriver  
import time
from selenium.webdriver.common.keys import Keys  
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import random

driver = webdriver.Chrome('ChromeDriver')
driver.get("https://devbusiness.tunai.io/login")
time.sleep(2)
driver.maximize_window()

#log in credentials
username = driver.find_element(By.NAME, "loginUsername");
username.send_keys("xxxxx");

password = driver.find_element(By.NAME, "loginPassword");
password.send_keys("xxxxx");

login = driver.find_element(By.XPATH,"//*[@id='app']/div/div/div/div/div/div[2]/form/div[4]/button");
login.submit();
time.sleep(3)

driver.get("https://devbusiness.tunai.io/dashboard/my_salon_user")
time.sleep(3)



for x in range(1):

 driver.find_element(By.XPATH,"//*[@id='__BVID__74']/tbody")
 random.choice(["dayon@tunai","dayon.salon3@tunai","dayonmanager@tunai"])
 time.sleep(5)

 element = driver.find_elements(By.CLASS_NAME,"custom-control custom-switch mt-2 mb-2")
 random.choice(element)
 element.click()
 time.sleep(3)

 driver.find_element(By.XPATH,'//*[@id="accAddPermission"]/div/div[1]/form/div[13]/button').submit()
 time.sleep(3)

 driver.find_element(By.XPATH,'//*[@id="modalClose"]').click()
 time.sleep(3)

Firstly, I define all items into an array and perform random choice, but it couldn't click on the either items, and next i used for loop, to random select items from the service again, it didn't works as well. Hope someone could assist me. I kinda run of idea...

CodePudding user response:

To click on specific username listed on table you need to use unique xpath to click.

After login added this code.

driver.get("https://devbusiness.tunai.io/dashboard/my_salon_user")
time.sleep(3)
randomtext = random.choice(["dayon@tunai","dayon.salon3@tunai","dayonmanager@tunai"])
element = driver.find_element(By.XPATH,"//table[@id='__BVID__74']/tbody//tr//td[2]//a[text()='{}']".format(randomtext))
element.click()

CodePudding user response:

The problem is that you have picked an option randomly from the list but haven't used it correctly. The usage random.choice(element) is incorrect.

You can use the below code as a reference for the problem that you are facing. Here //tbody[@role='rowgroup']/tr[@role='row']/td[2]/a is thy xpath for the table cell that contains all username fields (plus some other elements)

randomUsername = random.choice(["dayon@tunai","dayon.salon3@tunai","dayonmanager@tunai"])
driver.find_element(By.XPATH, "//tbody[@role='rowgroup']/tr[@role='row']/td/a[text()='" randonUsername "']").click()

in this first ine, this will first create a variable with a random value from your list. Then it will pass the variable with the randomized option in the xpath for the table cell and click it. A new dialog with "Blocked Permissions" will open

  • Related