需要帮助,以便使用XPATH和PhantomJS for python项目获取跨度内的文本吗?有人吗?

时间:2019-06-18 03:29:10

标签: python html selenium-webdriver xpath phantomjs

需要帮助!我正在尝试从下面的HTML中获取“ RC0402JR-070RL”。

这是HTML:

                        <td class="tr-mfgPartNumber">
                            <a itemprop="url" href="/product-detail/en/yageo/RC0402JR-070RL/311-0.0JRCT-ND/729353">
                                <span itemprop="name">RC0402JR-070RL</span>
                            </a>
                        </td>

我可以使用下面的xpath命令找到文本。已经使用指向正确文本的xpather.com进行了测试。

//table[@id='productTable']//td[contains(.,'Cut')]/preceding-sibling::td[@class='tr-mfgPartNumber']//span[@itemprop='name']

但是我无法使用以下命令在Python中打印它:

browser.find_element_by_xpath("//table[@id='productTable']//td[contains(.,'Cut')]/preceding-sibling::td[@class='tr-mfgPartNumber']//span").text

我可以用Python打印其他范围文本,但不能打印此文本。

有什么主意吗?这是我的Pyhthon代码:

from selenium import webdriver
browser = webdriver.PhantomJS(executable_path='C:/Phantom/phantomjs-2.1.1-windows/bin/phantomjs.exe')
browser.set_window_size(1124, 850)
url = "https://www.digikey.com/products/en?keywords=RC0402JR-070RL"


browser.find_element_by_xpath("//table[@id='productTable']//td[contains(.,'Cut')]/preceding-sibling::td[@class='tr-mfgPartNumber']//span").text

这是我收到的错误消息:

*

*Traceback (most recent call last):
  File "<pyshell#114>", line 1, in <module>
    browser.find_element_by_xpath("//table[@id='productTable']//td[contains(.,'Cut')]/preceding-sibling::td[@class='tr-mfgPartNumber']//span").text
  File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 365, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 843, in find_element
    'value': value})['value']
  File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 308, in execute
    self.error_handler.check_response(response)
  File "C:\Python\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: {"errorMessage":"Unable to find element with xpath '//table[@id='productTable']//td[contains(.,'Cut')]/preceding-sibling::td[@class='tr-mfgPartNumber']//span'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"189","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:54137","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"xpath\", \"value\": \"//table[@id='productTable']//td[contains(.,'Cut')]/preceding-sibling::td[@class='tr-mfgPartNumber']//span\", \"sessionId\": \"820657b0-90a8-11e9-9d2e-6935905b705f\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/820657b0-90a8-11e9-9d2e-6935905b705f/element"}}
Screenshot: available via screen*

*

1 个答案:

答案 0 :(得分:1)

进口需求。

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

等待存在的元素。

xpath = "//table[@id='productTable']//td[contains(.,'Cut')]/preceding-sibling::td[@class='tr-mfgPartNumber']//span"
# wait for the element to present max of 30 seconds
WebDriverWait(browser, 30).EC.presence_of_element_located((By.XPATH, xpath))
# now get the text
print(browser.find_element_by_xpath(xpath).text)