简体   繁体   English

Python Selenium 定位元件并点击它

[英]Python Selenium Locating Element and Clicking on it

I have been stuck in this line of code for over 2 weeks now, and I am hoping someone might come to my assistance!我已经被这行代码困了两个多星期了,我希望有人能来帮助我!

I am using python selenium to navigate through a website which contains a database.我正在使用 python selenium 浏览包含数据库的网站。 I want to click on a link which will open a new tab, from which I am going to extract the information that I am looking for.我想单击一个链接,该链接将打开一个新选项卡,我将从该选项卡中提取我正在寻找的信息。 I only need to do this once, so no need for any sort of loops, However, I have tried multiple times and I have been getting the ElementClickInterceptedException error message every time.我只需要这样做一次,因此不需要任何类型的循环,但是,我已经尝试了多次,并且每次都收到ElementClickInterceptedException错误消息。 I have tried using WebDriverWait , and then I get NoSuchElement error message.我尝试使用WebDriverWait ,然后收到NoSuchElement错误消息。 I've tried searching the element through several ways, including id, class name, xpath , and link text, but no luck!我尝试通过多种方式搜索元素,包括 id、class 名称、 xpath和链接文本,但没有运气!

The HTLM code looks like this: HTLM 代码如下所示:

 <a id="reportsForm:j_idt290:4:j_idt295" href="#" class="ui-commandlink ui-widget nav-item " onclick="PrimeFaces.addSubmitParam('reportsForm',{'reportsForm:j_idt290:4:j_idt295':'reportsForm:j_idt290:4:j_idt295'}).submit('reportsForm','_blank');return false;" target="_blank"><span id="reportsForm:j_idt290:4:l2" data-hasqtip="reportsForm:j_idt290:4:tooltipID" aria-describedby="qtip-reportsForm:j_idt290:4:tooltipID">Análise Financeira e de Gestão</span></a> <span id="reportsForm:j_idt290:4:l2" data-hasqtip="reportsForm:j_idt290:4:tooltipID" aria-describedby="qtip-reportsForm:j_idt290:4:tooltipID">Análise Financeira e de Gestão</span>

So far, I have tried:到目前为止,我已经尝试过:

afg = driver.find_element_by_link_text("Análise Financeira e de Gestão") 

afg = driver.find_element_by_xpath("//*[contains(text(), 'Análise Financeira e de Gestão')]")

WebDriverWait(driver,10).until(expected_conditions.element_to_be_clickable((By.ID,"reportsForm:j_idt290:4:j_idt295"))).click()

afg = driver.find_element_by_id("j_idt290:4:j_idt295") 

I think the problem might be because the link text is not immediately after the href , and not even in the same line, And so, when I search by link text.我认为问题可能是因为链接文本不是紧跟在href之后,甚至不在同一行中,所以,当我通过链接文本搜索时。 it can't be found...找不到...

Also, sometimes, without changing the code at all, just by running it again, with the driver.find_element_by_id method, it works out of nowhere once or twice, but then its stops working again!此外,有时,根本不更改代码,只需使用 driver.find_element_by_id 方法再次运行它,它就会突然运行一次或两次,但随后又停止运行!

Any ideas on how to solve this problem?关于如何解决这个问题的任何想法?

Thank you for your help:!!谢谢您的帮助:!! :) :)

You can try this xpath.你可以试试这个 xpath。

//a[normalize-space(.)="Análise Financeira e de Gestão"]

Maybe the element you're trying to hit is found within a frame, if that's the case you should switch to that iframe first and then you'll be able to perform the desired action on that element也许您尝试点击的元素在一个框架内找到,如果是这种情况,您应该先切换到该 iframe 然后您将能够对该元素执行所需的操作

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

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