简体   繁体   English

如何使用Selenium Python xpath单击某些按钮?

[英]How to click certain button using Selenium Python xpath ?

I am trying to scrap a web site News list after searching a certain word. 我正在搜索某个单词后尝试抓取网站“新闻列表”。 But I can't figure out how to access News list. 但是我不知道如何访问新闻列表。

First of all, I entered a key work and click search button. 首先,我输入了一个关键作品,然后单击“搜索”按钮。

elem = driver.find_element_by_id("query") 
elem.send_keys("kpop")    
elem = driver.find_element_by_id("search_btn")
elem.click()

The result page is this: https://search.naver.com/search.naver?sm=tab_hty.top&where=nexearch&query=kpop&oquery=kpop&tqi=To2nllpySEKsssnbxrsssssssFd-407861 结果页面是这样的: https : //search.naver.com/search.naver?sm=tab_hty.top&where=nexearch&query=kpop&oquery=kpop&tqi=To2nllpySEKsssnbxrsssssssssFd-407861

And I want to move on News tab placed on third tab 我想移至第三个标签上的“新闻”标签 新闻标签

Please help! 请帮忙!

Right-click the desired element. 右键单击所需的元素。 Copy the xpath. 复制xpath。 Paste it in .find_element_by_xpath() . 将其粘贴到.find_element_by_xpath()

button = driver.find_element_by_xpath('//*[@id="nx_search_form"]/fieldset/button ') button = driver.find_element_by_xpath('//*[@id="nx_search_form"]/fieldset/button ')

As per the url you have shared to click on the link to move on to the News tab placed on third tab you can use the following line of code : 根据您共享的URL ,单击链接可转到位于第三个选项卡上的“ 新闻”选项卡,您可以使用以下代码行:

driver.find_element_by_xpath("//ul[@class='base']/li[@class='lnb4']/a[@class='tab']/span[@class='spnew']").click()

You may require to induce WebDriverWait for the webelement to be clickable as follows : 您可能需要诱使WebDriverWait来使该webeable可单击,如下所示:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='base']/li[@class='lnb4']/a[@class='tab']/span[@class='spnew']"))).click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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