Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.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
Javascript 如何在Python中使用Selenium找到找到所需元素的正确方法?_Javascript_Python_Selenium_Xpath - Fatal编程技术网

Javascript 如何在Python中使用Selenium找到找到所需元素的正确方法?

Javascript 如何在Python中使用Selenium找到找到所需元素的正确方法?,javascript,python,selenium,xpath,Javascript,Python,Selenium,Xpath,目标: <li><a href="javascript:void(0);" ng-click="vm.exportData('csv')"><i class="fa fa-file-text"></i> csv</a></li> 我写的代码是: LINK = "http://e-licitatie.ro/pub/notices/contract-award-notices/list/3/1" PATH = "/Users/

目标:

<li><a href="javascript:void(0);" ng-click="vm.exportData('csv')"><i class="fa fa-file-text"></i> csv</a></li>
我写的代码是:

LINK = "http://e-licitatie.ro/pub/notices/contract-award-notices/list/3/1"
PATH = "/Users/ultiplex/Documents/WORKSPACE/insightout/chromedriver"
DRIVER = webdriver.Chrome(PATH)
DELAY = 30

def wait(delay: int, xpath: str) -> None: 
    try:
        WebDriverWait(DRIVER, delay).until(EC.invisibility_of_element_located((By.XPATH, "//div[@class='block-ui-overlay']")))
        print('Overlay bypassed')
        WebDriverWait(DRIVER, delay).until(EC.presence_of_element_located((By.XPATH, xpath)))
        print(f"Element {xpath} found")
    except: 
        print("Error")

def load_link(link: str) -> None: 
    DRIVER.get(LINK)
    print("Link loaded")    

def click(xpath: str, delay: int) -> None: 
    wait(delay, xpath)
    DRIVER.find_element_by_xpath(xpath).click()
    print("Clicked")

def filter(fr_date: str, to_date: str) -> None: 
    DRIVER.find_element_by_xpath("//input[@placeholder='De la data']").send_keys(fr_date)
    DRIVER.find_element_by_xpath("//input[@placeholder='Pana la data']").send_keys(to_date)
    print("Filter selected")

if __name__ == "__main__":
    load_link(LINK)
    click("//button[@class='pull-right btn btn-default']", DELAY)
    filter("31/03/2020", "01/04/2020")
    click("//button[@class='pull-right margin-left-5 btn btn-entity']", DELAY)
单击在上面提到的示例中起作用,但我找不到方法使其在最后一步导出中起作用

请支持

试试下面的xpath

(//ul[@class='dropdown-menu'][2]//a)[3]

尝试下面的xpath

(//ul[@class='dropdown-menu'][2]//a)[3]


您需要首先单击导出链接以查看列表元素,然后才能单击csv链接。单击搜索后添加两行

#Click on Export link
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[@class='darkorange dropdown-toggle' and contains(.,'Export')]"))).click()
#Click on csv link
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//ul[@class='dropdown-menu']//li//a[contains(.,'csv')]"))).click()
完整代码块

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

driver=webdriver.Chrome()
driver.get("http://e-licitatie.ro/pub/notices/contract-award-notices/list/3/1")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[@class='pull-right btn btn-default']"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//input[@placeholder='De la data']"))).send_keys("31/03/2020")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//input[@placeholder='Pana la data']"))).send_keys("01/04/2020")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[@class='pull-right margin-left-5 btn btn-entity']"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[@class='darkorange dropdown-toggle' and contains(.,'Export')]"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//ul[@class='dropdown-menu']//li//a[contains(.,'csv')]"))).click()

您需要首先单击导出链接以查看列表元素,然后才能单击csv链接。单击搜索后添加两行

#Click on Export link
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[@class='darkorange dropdown-toggle' and contains(.,'Export')]"))).click()
#Click on csv link
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//ul[@class='dropdown-menu']//li//a[contains(.,'csv')]"))).click()
完整代码块

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

driver=webdriver.Chrome()
driver.get("http://e-licitatie.ro/pub/notices/contract-award-notices/list/3/1")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[@class='pull-right btn btn-default']"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//input[@placeholder='De la data']"))).send_keys("31/03/2020")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//input[@placeholder='Pana la data']"))).send_keys("01/04/2020")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[@class='pull-right margin-left-5 btn btn-entity']"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[@class='darkorange dropdown-toggle' and contains(.,'Export')]"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//ul[@class='dropdown-menu']//li//a[contains(.,'csv')]"))).click()

第一个:引发异常(message,screen,stacktrace)selenium.common.exceptions.NoSuchElementException:message:没有这样的元素:找不到元素:{“方法”:“xpath”,“选择器”:“(//ul ul[@class='dropdown-menu'][2]//a)[3]}第二个:引发异常(message screen,stacktrace)selenium.common.exceptions.NoSuchElementException:Message:没有这样的元素:找不到元素:{“方法”:“xpath”,“选择器”:”//ul[@class='dropdown-menu'][2]//a[normalize space(.)='csv']}我知道页面上有3个“dropdown menu”。这对你的建议有影响吗?你必须针对第二个下拉菜单。(用于PDF导出):
(//ul[@class=“dropdown menu”])[2]/li[2]/a
最短形式:
(//a[contains(@ng click,'PDF')])[1]
在第一个XPath中将'li[2]'替换为'li[3]',如果您希望使用csv而不是PDF导出,则将'PDF'替换为'csv'。第一:引发异常类(消息、屏幕、堆栈跟踪)selenium.common.exceptions.NoSuchElementException:Message:没有这样的元素:无法定位元素:{“方法”:“xpath”,“选择器”:“(//ul[@class='dropdown-menu'][2]//a][3]”}秒:引发异常类(Message,screen,stacktrace)selenium.common.exceptions.NoSuchElementException:Message:没有这样的元素:无法定位元素:{“方法”:“xpath”、“选择器”:”//ul[@class='dropdown-menu'][2]//a[normalize space(..='csv'])我知道页面上有3个“dropdown menu”。这对您的建议有影响吗?您必须针对第二个下拉菜单。这应该可以(用于PDF导出):
(//ul ul class='dropdown-menu'])[2]/li[2]/a
最短形式:
(//a[contains>)(@ng click,'pdf')][1]
如果你想要csv而不是pdf导出,那么在第一个XPath中将'li[2]'替换为'li[3]',将'pdf'替换为'csv'.没有什么比一个明显的事实更具欺骗性了。谢谢你,善良的先生!