Python Selenium-WebDriver - 如何识别这个 Button/Link
Python Selenium-WebDriver - How to Identify this Button/Link
请参考下面的截图和HTML代码。有两个 buttons/links 的名称中包含“Indiana -4.5”和“Indiana”。我正在尝试确定第一个也有“-4.5”的数字,但是我不知道这个数字在变化时是什么。名称“Indiana”后可以跟“+”、“-”,然后是一位或两位十进制数。
仅给定“Indiana”作为变量,我如何select“Indiana -4.5”而不是“Indiana”link/button。谢谢!
Link 到页面:https://prolineplus.olg.ca/en-ca/event/?e64240-NBA-Basketball-USA-Indiana-Charlotte
def proline_go_to_match2(driver, team):
team = "Indiana"
print(team)
try:
match = WebDriverWait(driver, 15).until(
EC.presence_of_element_located((By.XPATH, "//*[@title= '" + team + "']"))
)
match.click()
except:
driver.quit()
我不知道如何让 Seleium 识别这个 link,非常感谢你的帮助!
这是 HTML 元素
这是第一个link/button:
<span class="fdjgs-outcome-wrapper" aria-pressed="true" role="button" tabindex="0" aria-label="Bet for Charlotte @ Indiana - Indiana -4.5 at 2.10" title="Indiana -4.5"><span class="fdjgs-description">Indiana -4.5</span><span class="fdjgs-price">2.10</span></span>
这是第二个link/button:
<li class="fdjgs-outcome" title="Indiana" data-state="unselected" data-direction="" data-unpriced="false" data-hidden="false" data-resulted="false" data-suspended="false"><span class="fdjgs-outcome-wrapper" aria-pressed="false" role="button" tabindex="0" aria-label="Bet for Charlotte @ Indiana - Indiana at 1.65" title="Indiana"><span class="fdjgs-description">Indiana</span><span class="fdjgs-price">1.65</span></span></li>
点击元素 Indiana -4.5 而不是 you need to induce WebDriverWait for the and you can use the following :
使用 XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='fdjgs-markets']/li[@class='fdjgs-market']//ul[@class='fdjgs-outcomes']//descendant::li[2]//span[@class='fdjgs-outcome-wrapper' and contains(@aria-label, 'Indiana')]/span[starts-with(., 'Indiana')]"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
浏览器快照:
您应该使用 visibility_of_element_located
而不是 presence_of_element_located
。
使用正确的定位器,您的代码可以如下所示:
def proline_go_to_match2(driver, team):
team = "Indiana"
print(team)
try:
match = WebDriverWait(driver, 15).until(EC.visibility_of_element_located((By.XPATH, "//header[.//span[text()='Point Spread'] and .//span[contains(.,'Match')]]/..//li[contains(@title,'" + team + "')]")) )
match.click()
except:
driver.quit()
请参考下面的截图和HTML代码。有两个 buttons/links 的名称中包含“Indiana -4.5”和“Indiana”。我正在尝试确定第一个也有“-4.5”的数字,但是我不知道这个数字在变化时是什么。名称“Indiana”后可以跟“+”、“-”,然后是一位或两位十进制数。
仅给定“Indiana”作为变量,我如何select“Indiana -4.5”而不是“Indiana”link/button。谢谢!
Link 到页面:https://prolineplus.olg.ca/en-ca/event/?e64240-NBA-Basketball-USA-Indiana-Charlotte
def proline_go_to_match2(driver, team):
team = "Indiana"
print(team)
try:
match = WebDriverWait(driver, 15).until(
EC.presence_of_element_located((By.XPATH, "//*[@title= '" + team + "']"))
)
match.click()
except:
driver.quit()
我不知道如何让 Seleium 识别这个 link,非常感谢你的帮助!
这是 HTML 元素
这是第一个link/button:
<span class="fdjgs-outcome-wrapper" aria-pressed="true" role="button" tabindex="0" aria-label="Bet for Charlotte @ Indiana - Indiana -4.5 at 2.10" title="Indiana -4.5"><span class="fdjgs-description">Indiana -4.5</span><span class="fdjgs-price">2.10</span></span>
这是第二个link/button:
<li class="fdjgs-outcome" title="Indiana" data-state="unselected" data-direction="" data-unpriced="false" data-hidden="false" data-resulted="false" data-suspended="false"><span class="fdjgs-outcome-wrapper" aria-pressed="false" role="button" tabindex="0" aria-label="Bet for Charlotte @ Indiana - Indiana at 1.65" title="Indiana"><span class="fdjgs-description">Indiana</span><span class="fdjgs-price">1.65</span></span></li>
点击元素 Indiana -4.5 而不是
使用 XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='fdjgs-markets']/li[@class='fdjgs-market']//ul[@class='fdjgs-outcomes']//descendant::li[2]//span[@class='fdjgs-outcome-wrapper' and contains(@aria-label, 'Indiana')]/span[starts-with(., 'Indiana')]"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
浏览器快照:
您应该使用 visibility_of_element_located
而不是 presence_of_element_located
。
使用正确的定位器,您的代码可以如下所示:
def proline_go_to_match2(driver, team):
team = "Indiana"
print(team)
try:
match = WebDriverWait(driver, 15).until(EC.visibility_of_element_located((By.XPATH, "//header[.//span[text()='Point Spread'] and .//span[contains(.,'Match')]]/..//li[contains(@title,'" + team + "')]")) )
match.click()
except:
driver.quit()