Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.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
Python 如何获取某些标记值_Python_Html_Selenium_Automation - Fatal编程技术网

Python 如何获取某些标记值

Python 如何获取某些标记值,python,html,selenium,automation,Python,Html,Selenium,Automation,以下脚本将正确打开给定的URL,并打开“查看发票”选项卡,该选项卡将打开详细发票。 我需要从详细发票中获取一些值,如编号、日期和表格值。这些值是非常嵌套的,以便正确地访问它们。单击查看发票时打开的URL,我不知道如何刮取它或使用selenium继续。 代码中的元素类似于获取打开的详细发票页面的值的实例,还是有其他方法 代码如下: from selenium import webdriver driver = webdriver.Chrome(executable_path=r'D:/Chro

以下脚本将正确打开给定的URL,并打开“查看发票”选项卡,该选项卡将打开详细发票。 我需要从详细发票中获取一些值,如编号、日期和表格值。这些值是非常嵌套的,以便正确地访问它们。单击查看发票时打开的URL,我不知道如何刮取它或使用selenium继续。 代码中的元素类似于获取打开的详细发票页面的值的实例,还是有其他方法

代码如下:

from selenium import webdriver


driver = webdriver.Chrome(executable_path=r'D:/Chrome driver/chromedriver.exe') # Get local session(use webdriver.Chrome() for chrome) 
driver.implicitly_wait(3)

driver.get("URL") # load page from some url

driver.find_element_by_xpath("//input[@id='PNRId']").send_keys("MHUISZ")
driver.find_element_by_xpath("//input[@id='GstRetrievePageInteraction']").click()

element = driver.find_element_by_name('ViewInvoice')
element.click()

是否有人可以指导我如何从发票页面获取值?

因此,请尝试等待元素可见或可单击,您单击发票实际上会创建新的子句柄,因此您必须切换到它们。您现在所要做的就是找出如何遍历一个表,并尝试查看它的xpath

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path=r'D:/Chrome driver/chromedriver.exe') # Get local session(use webdriver.Chrome() for chrome) 

driver.get("URL") 
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//input[@id='PNRId']"))).send_keys("MHUISZ")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='GstRetrievePageInteraction']"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, 'ViewInvoice'))).click()
p = driver.current_window_handle
#get first child window
chwnd = driver.window_handles
for w in chwnd:
   #switch focus to child window
   if(w!=p):
       driver.switch_to.window(w)
       break
invoiceTable = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "TableHeader")))
print(invoiceTable.find_element_by_xpath("tbody/tr[1]/td").text)
driver.quit()

您好,如果您想简单地获取这些标记的值,请首先找到元素。然后使用.text.Hi Arundeep来查找标记,我使用元素来查找标记,并使用了.text方法。但它给出了NoTouchElementException:没有这样的元素:无法定位元素。我是否应该使用此“element=driver.find_element\u by_name('ViewInvoice')”元素来获取值?当您获得NoTouchElement异常时,可能有多种原因导致您无法获取该异常,请尝试将driver.findbyelemet包装到webdriver中。请稍候。您想在下一页获取值。element.click()打开一个详细的页面,我需要获取该页面的值,我应该使用什么定位器来使用元素或驱动程序?我试着用wait's not work.ok来包装它,所以element.click()是一个新的句柄,我必须打开它才能访问该窗口中的元素。谢谢,成功了。以类似的方式,我应该得到其他值。diver.window\u handles是否获取所有打开的窗口?这样您就可以切换回。如果您需要其他人的帮助,请将答案标记为已接受,只需评论。确定。谢谢。你能看看这个吗