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
1 Answer
Reset to default 0You 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
版权声明:本文标题:python - How to assign a variable to a true value of an if statement - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742339911a2456389.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论