Python Selenium按xpath查找元素,点击似乎不起作用

时间:2018-05-10 10:35:31

标签: python selenium

我发现很少有关于这个主题的问题,但遗憾的是,这对我没有帮助。

我的代码

def gopro(url):
    driver = webdriver.Chrome() ##PhantomJS() <- that didnt work either
    driver.get(url)
    time.sleep(4)
    driver.implicitly_wait(5)
    html = driver.page_source
    soup = BeautifulSoup(html, 'html.parser')
    driver.find_element_by_xpath('//*[@id="gpn-header"]/div[2]/div/div/div[2]/div/div[2]/div[2]/div/select/option[285]').click()
    time.sleep(2)
    driver.navigate().refresh()
    obj = soup.find('h1', {'class':'price-sales notranslate'})
    print(obj)


print(gopro('https://shop.gopro.com/Marketing/cameras/hero5-black/CHDHX-502-master.html'))

我也使用过Chromebrowser,但也没用 我想在网站上做的是获得gopro的价格,但我必须更改货币(我需要4种货币(美元,欧元,英镑和挪威克朗),它位于下拉表格下。 更新: 发布截图以明确说明,我希望右上角的语言/货币从欧元变为英镑 here is what i want

enter image description here

1 个答案:

答案 0 :(得分:0)

我想,您正在尝试将位置值更改为英国(GB),英语并获取其价格值。

def gopro(url):
    driver = webdriver.Chrome() ##PhantomJS() <- that didnt work either
    driver.get(url)
    time.sleep(4)
    driver.implicitly_wait(5)
    html = driver.page_source
    soup = BeautifulSoup(html, 'html.parser')
    driver.find_element_by_css_selector('.gpn-locale-select-current-country-text').click()
    Select(driver.find_element_by_css_selector('.gpn-locale-select-selector')).select_by_visible_text('United Kingdom (GB), English')
    time.sleep(2)
    driver.navigate().refresh()
    obj = soup.find('h1', {'class':'price-sales notranslate'})
    print(obj)


print(gopro('https://shop.gopro.com/Marketing/cameras/hero5-black/CHDHX-502-master.html'))

在这里,我试图点击下拉列表并选择值。