我想提取“BMW Serie 2 Active Tourer” 表,第一列,通过单击https://motorgiga.com/bmw/2-series中的“MOSTRAR VERSIONES (14)” 出现。
Xpath '//div[@data-nql="ve:car:bmw:serie-2-active-tourer"]' 有效。
Xpath '//div[@data-nql="ve:car:bmw:serie-2-active-tourer"]/table/tbody/tr[1]/td[1]/a' 不起作用。
您能帮我正确编写 Xpath 语法以到达这里吗?
该脚本尝试通过标签“serie-2-active-tourer”获取表格,因为我想将每个表格与其模型相关联。
Pd:我的屏幕截图在最后,显示了 html。
谢谢,
何塞·路易斯
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--window-size=1920x1080")
chrome_options.add_argument('--headless') # ESTO ES OROOOO
chrome_options.add_argument('--disable-gpu') # ESTO ES OROOOO :)
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=r"chromedriver_win32/chromedriver.exe")
url = 'https://motorgiga.com/bmw/2-series/precios'
driver.get(url)
# 1. This works
xpath = driver.find_elements_by_xpath('//div[@data-nql="ve:car:bmw:serie-2-active-tourer"]')
result = [xpath[i].get_attribute('class') for i in range(0, len(list(xpath)))]
print(result)
# 2. Now we enter into the table division. This doesn't work
xpath = driver.find_elements_by_xpath(
'//div[@data-nql="ve:car:bmw:serie-2-active-tourer"]/table/tbody/tr[1]/td[1]/a')
我的 html 屏幕截图:www.i.stack.imgur.com/Z0V0U.png