Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未找到元素-Selenium-Python_Python_Selenium_Xpath - Fatal编程技术网

未找到元素-Selenium-Python

未找到元素-Selenium-Python,python,selenium,xpath,Python,Selenium,Xpath,我试图进入曼联的主场,但不可能进入。 下面是HTML页面的一段代码: <div id="g_1_l2dtbMED" title="Click for match detail!" class="event__match event__match--static event__match--last event__match--oneLine"><div class="event__time">

我试图进入曼联的主场,但不可能进入。

下面是HTML页面的一段代码:

<div id="g_1_l2dtbMED" title="Click for match detail!" class="event__match event__match--static event__match--last event__match--oneLine"><div class="event__time">04.10. 17:30</div><div class="event__participant event__participant--home"><svg class="card___2ip_DLm icon--redCard icon--redCard-first icon--redCard-last"><title></title><use xlink:href="/res/_fs/build/symbols.f1bc6b2.svg#card"></use></svg>Manchester Utd</div><div class="event__scores fontBold"><span>1</span>&nbsp;-&nbsp;<span>6</span></div><div class="event__participant event__participant--away fontBold">Tottenham</div><div class="event__part">(1&nbsp;-&nbsp;4)</div><span class="wld wld--l" title="Loss">L</span></div>
但这会在20秒后抛出异常“TimeoutException”,这是搜索的时间限制。

试试这个xpath-

//div[contains(text(),'Manchester Utd')]/following-sibling::span
试试这个xpath-

//div[contains(text(),'Manchester Utd')]/following-sibling::span

您正在查找的定位器是

//div[contains(@class,'event__participant--home')][text()='Manchester Utd']//following-sibling::span[1]
^ find a DIV that contains the class indicating a home game
                                                  ^ that also contains the team name
                                                                           ^ then find the first sibling SPAN that follows
该定位器将仅为家庭游戏查找包含L、W、D等的元素

如果要等待元素,则需要等待可见而不是存在。存在是指元素仅在DOM中,但不一定可见。如果要从页面上删除文本,则需要等待可见。您可以使用
EC.visibility\u of \u all \u elements\u located()
来实现这一点。看见如果在页面存在但不可见时尝试刮除页面,则会引发异常

您的更新代码如下

driver = webdriver.Chrome()
url = "https://www.flashscore.com/team/manchester-united/ppjDR086/results/"
driver.get(url)

Team = 'Manchester Utd'
results = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH,"//div[contains(@class,'event__participant--home')][text()='" + Team + "']//following-sibling::span[1]")))
print(len(results))

您正在查找的定位器是

//div[contains(@class,'event__participant--home')][text()='Manchester Utd']//following-sibling::span[1]
^ find a DIV that contains the class indicating a home game
                                                  ^ that also contains the team name
                                                                           ^ then find the first sibling SPAN that follows
该定位器将仅为家庭游戏查找包含L、W、D等的元素

如果要等待元素,则需要等待可见而不是存在。存在是指元素仅在DOM中,但不一定可见。如果要从页面上删除文本,则需要等待可见。您可以使用
EC.visibility\u of \u all \u elements\u located()
来实现这一点。看见如果在页面存在但不可见时尝试刮除页面,则会引发异常

您的更新代码如下

driver = webdriver.Chrome()
url = "https://www.flashscore.com/team/manchester-united/ppjDR086/results/"
driver.get(url)

Team = 'Manchester Utd'
results = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH,"//div[contains(@class,'event__participant--home')][text()='" + Team + "']//following-sibling::span[1]")))
print(len(results))

//div[@class='event\uu participant--home']拼写错误?不,这是故意的,因为结果必须是包含此名称类和文本“Team”的应答器的兄弟。它必须满足双重条件。这个函数不是我使用的find_elements(),而是找到元素的可见性_located()'manchester Utd'应该是'manchester Utd'也找到元素应该是一个预期条件。EC.presence_of_all_elements_找到了您等待的条件,//div[@class='event_participant--home']typo No,这是故意的,因为结果必须是包含此名称类和文本“Team”的应答器的兄弟。它必须满足双重条件。这个函数不是我使用的find_elements(),而是_element_located()的可见性_'manchester Utd'应该是'manchester Utd'并且find_elements应该是一个预期的条件。EC.presence_of_all_elements_找到了您正在等待的条件,对不起,这不能解决我的问题,因为这段代码接受Man.Utd的所有结果。结果在家里和在家里。对不起,这并不能解决我的问题,因为这段代码获取了Man.utd的所有结果。无论是客场还是主场。