admin管理员组

文章数量:1334684

I am creating a tool to automatically fill out web forms across many websites. My current code to find the entry fields is:

try:
  Select_Box_Name = (driver.find_element(By.NAME, 'your-name')
  Select_Box_Name.click()
  time.sleep(1)
  Select_Box_Name.send_keys(FullName)
except: 
  print("Invalid")

This works for a single website, but, since different websites use different labels/names/etc., I need to have "Select_Box_Name.send_keys(FullName)" be applied to all the different possibilities.

The solution I initially thought of is using an if statement to check if a field is present/true. Here is the code I have so far:

if driver.find_element(By.NAME, 'your-name') or driver.find_element(by.NAME, 'full-name'):
  print("Found")

What I want to do is to assign the "Select_Box_Name" variable to whatever the truthy value of the if statement is. Is this possible or is there an alternative method I can apply? Thanks!

I am creating a tool to automatically fill out web forms across many websites. My current code to find the entry fields is:

try:
  Select_Box_Name = (driver.find_element(By.NAME, 'your-name')
  Select_Box_Name.click()
  time.sleep(1)
  Select_Box_Name.send_keys(FullName)
except: 
  print("Invalid")

This works for a single website, but, since different websites use different labels/names/etc., I need to have "Select_Box_Name.send_keys(FullName)" be applied to all the different possibilities.

The solution I initially thought of is using an if statement to check if a field is present/true. Here is the code I have so far:

if driver.find_element(By.NAME, 'your-name') or driver.find_element(by.NAME, 'full-name'):
  print("Found")

What I want to do is to assign the "Select_Box_Name" variable to whatever the truthy value of the if statement is. Is this possible or is there an alternative method I can apply? Thanks!

Share Improve this question asked Nov 20, 2024 at 17:45 PsychedelicSealPsychedelicSeal 356 bronze badges 1
  • to see if you found an element use find_elements... if the array returned has a size of zero, then it was not found. Depending on the site, you may want a sleep first, or a webdriverwait... – browsermator Commented Nov 20, 2024 at 18:01
Add a comment  | 

1 Answer 1

Reset to default 0

You can assign the value of Select_Box_Name to the expression in the if statement

Select_Box_Name = driver.find_element(By.NAME, 'your-name') or driver.find_element(by.NAME, 'full-name'

and then do

if Select_Box_name: print("Found")

本文标签: pythonHow to assign a variable to a true value of an if statementStack Overflow