简体   繁体   中英

Need help to get text inside span using XPATH and PhantomJS for python project? Anyone?

Need Help! I am trying to grab "RC0402JR-070RL" from the HTML below.

Here is the 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>

I am able to locate the text using xpath command below. Already tested using xpather.com which point to the right text.

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

But I am unable to print it in Python using command below:

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

I can print other span texts in Python but not this one.

Any idea? Here is my Pyhthon code:

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

Here is the Error Message I get:

*

*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*

*

Imports need.

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

Wait for the element present.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM