如果条件满足,识别并单击无序列表中的按钮
Identify and click button in unordered list if condition met
我正在使用 Python/Selenium
访问网页上的 unordered
列表。我已经使用 find_element_by_xpath
正确识别了我要查找的 div(text "$x.xx"
几乎总是唯一的)。
然后我想指示代码按下驻留在我确定的 div 中的购买按钮(假定同一列表上还有 100 个其他“购买”按钮)。
但是有一个条件,在下面的两个例子中,其中一个 'a classes' includes the text "disabled".
如果 div 和正确的文本已经被识别,但是 'a class' includes "disabled"
,我会喜欢脚本循环并在 1 秒后再次 运行。
总而言之,如果我搜索低于 50.00 美元的代码将会循环,如果我正在搜索 30.00 美元它会点击()那个特定 div 中的“购买”按钮。
如有任何帮助,我们将不胜感激。
(第一次发帖,如果格式不正确请见谅!)
Python代码:
from selenium import webdriver
PATH = "C:\Users\abc\Desktop\Automation\chromedriver.exe"
driver = webdriver.Chrome(PATH)
findClick = driver.find_element_by_xpath("//*[text()='.00']")
网页:
<ul>
<li class="List-row">
<div class="List-rowContent Market">
<div class="Market-item Total"><span>.00</span></div>
<div class="Market-item Buy">
<a class="Button-small Market-Buy disabled" data-dialog="#buy_start">Purchase</a></div></div></li>
<li class="List-row">
<div class="List-rowContent Market">
<div class="Market-item Total"><span>.00</span></div>
<div class="Market-item Buy">
<a class="Button-small Market-Buy" data-dialog="#buy_start">Purchase</a></div></div></li>
根据您分享的HTML
,以下xpath
//span[text()='.00']/../following-sibling::div/a[text()='Purchase']
表示基于价格文本的Purchase
按钮,您可以将其更改为50$
并签入HTMLDOM
。
现在来回答这个问题 :
There is one condition however, in the two examples below, one of the
'a classes' includes the text "disabled". If the div with the correct
text has been identified, but the 'a class' includes "disabled", I
would like the script to loop and run again 1 second later.
如果我理解正确,下面的代码应该可以工作。
try:
if len(driver.find_elements(By.XPATH, "//span[text()='.00']/../following-sibling::div/a[text()='Purchase']")) > 0:
print(".00 is visible so bot should click it directly")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='.00']/../following-sibling::div/a[text()='Purchase']"))).click()
else:
print(".00 was not visible so loop it here")
for i in range(100):
print("Inside else for loop for the nth time", i)
time.sleep(20) # for each iteration wait for 20 secs
#code logic here
except:
print("Something went wrong")
pass
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
我正在使用 Python/Selenium
访问网页上的 unordered
列表。我已经使用 find_element_by_xpath
正确识别了我要查找的 div(text "$x.xx"
几乎总是唯一的)。
然后我想指示代码按下驻留在我确定的 div 中的购买按钮(假定同一列表上还有 100 个其他“购买”按钮)。
但是有一个条件,在下面的两个例子中,其中一个 'a classes' includes the text "disabled".
如果 div 和正确的文本已经被识别,但是 'a class' includes "disabled"
,我会喜欢脚本循环并在 1 秒后再次 运行。
总而言之,如果我搜索低于 50.00 美元的代码将会循环,如果我正在搜索 30.00 美元它会点击()那个特定 div 中的“购买”按钮。
如有任何帮助,我们将不胜感激。
(第一次发帖,如果格式不正确请见谅!)
Python代码:
from selenium import webdriver
PATH = "C:\Users\abc\Desktop\Automation\chromedriver.exe"
driver = webdriver.Chrome(PATH)
findClick = driver.find_element_by_xpath("//*[text()='.00']")
网页:
<ul>
<li class="List-row">
<div class="List-rowContent Market">
<div class="Market-item Total"><span>.00</span></div>
<div class="Market-item Buy">
<a class="Button-small Market-Buy disabled" data-dialog="#buy_start">Purchase</a></div></div></li>
<li class="List-row">
<div class="List-rowContent Market">
<div class="Market-item Total"><span>.00</span></div>
<div class="Market-item Buy">
<a class="Button-small Market-Buy" data-dialog="#buy_start">Purchase</a></div></div></li>
根据您分享的HTML
,以下xpath
//span[text()='.00']/../following-sibling::div/a[text()='Purchase']
表示基于价格文本的Purchase
按钮,您可以将其更改为50$
并签入HTMLDOM
。
现在来回答这个问题 :
There is one condition however, in the two examples below, one of the 'a classes' includes the text "disabled". If the div with the correct text has been identified, but the 'a class' includes "disabled", I would like the script to loop and run again 1 second later.
如果我理解正确,下面的代码应该可以工作。
try:
if len(driver.find_elements(By.XPATH, "//span[text()='.00']/../following-sibling::div/a[text()='Purchase']")) > 0:
print(".00 is visible so bot should click it directly")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='.00']/../following-sibling::div/a[text()='Purchase']"))).click()
else:
print(".00 was not visible so loop it here")
for i in range(100):
print("Inside else for loop for the nth time", i)
time.sleep(20) # for each iteration wait for 20 secs
#code logic here
except:
print("Something went wrong")
pass
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC