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的table/tbody中单击()以删除每个元素?_Python_Selenium_Web Scraping_Selenium Chromedriver - Fatal编程技术网

如何在Selenium Python的table/tbody中单击()以删除每个元素?

如何在Selenium Python的table/tbody中单击()以删除每个元素?,python,selenium,web-scraping,selenium-chromedriver,Python,Selenium,Web Scraping,Selenium Chromedriver,我有一个包含多个链接的表,这些链接具有相同的LinkText,因此当我使用该表时,始终选择第一个元素,因此不起作用: driver.find_element_by_partial_link_text('Click here').click() 然后,我使用I javascript函数使用其XPath获取表/tbody中的所有元素。它可以工作,如果我用变量rows打印每个元素,它看起来 像这样 下面是我当前的代码: import time from selenium import webdriv

我有一个包含多个链接的表,这些链接具有相同的
LinkText
,因此当我使用该表时,始终选择第一个元素,因此不起作用:

driver.find_element_by_partial_link_text('Click here').click()
然后,我使用I javascript函数使用其XPath获取
表/tbody
中的所有元素。它可以工作,如果我用变量
rows
打印每个元素,它看起来 像这样

下面是我当前的代码:

import time
from selenium import webdriver

url="http://example_url.com"
driver_path="/driver/chromedriver"
driver = webdriver.Chrome(driver_path)
driver.get (url)

rows = driver.execute_script('''function getElementByXpath(path) {..};return getElementByXpath("//*[@id='someID']/table/tbody/").rows''')

>>> for r in rows:
...     print r # This prints the elements within 'rows'
...     #some other code
...
<selenium.webdriver.remote.webelement.WebElement (session="e819b7b267ba043d4233e118c5844e1e", element="0.7287146883212632-2")>
<selenium.webdriver.remote.webelement.WebElement (session="e819b7b267ba043d4233e118c5844e1e", element="0.7287146883212632-3")>
<selenium.webdriver.remote.webelement.WebElement (session="e819b7b267ba043d4233e118c5844e1e", element="0.7287146883212632-4")>
<selenium.webdriver.remote.webelement.WebElement (session="e819b7b267ba043d4233e118c5844e1e", element="0.7287146883212632-5")>
导入时间
从selenium导入webdriver
url=”http://example_url.com"
驱动程序路径=“/driver/chromedriver”
driver=webdriver.Chrome(驱动程序路径)
driver.get(url)
rows=driver.execute_脚本(''function getElementByXpath(path){..};return getElementByXpath(“/*[@id='someID']]/table/tbody/”).rows')
>>>对于行中的r:
...     打印r#打印“行”中的元素
...     #其他代码
...
如何对找到的每个元素执行
click()

比如:

对于行中的r: 打印r.单击()#这不起作用


感谢您的帮助。

您必须使用索引访问该行,因为每次单击表中的链接时,元素索引都将刷新。如果不使用索引并尝试使用循环单击链接,则最终可能会得到StaleElementException

下面是应该起作用的逻辑

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC


url="http://example_url.com"
driver_path="/driver/chromedriver.exe"
driver = webdriver.Chrome(driver_path)
driver.get (url)

numberOfRows = len(driver.find_elements_by_xpath("//*[@id='someID']/table/tbody//tr"))

for iRow in range(numberOfRows):

    # wait until the row is present (you need this when you are coming back to the row containing table
    currentRow = WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"(//*[@id='someID']/table/tbody//tr)[str(" + (iRow+1) + ")]")))
    # if you want to access the link in the row
    linkInCurrentRow = currentRow.find_elements_by_xpath(".//a[@attribute='attribute_value']")
    # click on the link or you can perform  desired operation 
    linkInCurrentRow.click()
    #write the logic below to navigate to the table containing page
    driver.back()

你能发布你的html源代码吗?如果您打算只单击每个已识别的元素,那么您应该只使用findElements方法并循环遍历每个元素来单击它。感谢您以复数形式提供有关
findElements
的建议。我发现存在这种方法,并且对我的
驱动程序有效。通过链接文本(“一些文本”)查找元素
。非常感谢。很好的例子,很高兴看到另一种方法/逻辑来做我想做的事情。非常感谢,谢谢