[英]how to click a link using selenium driver in python while don't know the text inside anchor tag
I use selenium+phantomJS+scrapy
to scrapy javascript content.我使用selenium+phantomJS+scrapy
来抓取 javascript 内容。 And i want to click a link in the table after loading the javascript content.我想在加载 javascript 内容后单击表中的链接。 But the most important thing is that with different input content, the link i want to click changed.但最重要的是,随着输入内容的不同,我想点击的链接发生了变化。
then i want to use xpath to locate the link but i failed.然后我想使用 xpath 来定位链接,但我失败了。 Here's the html element and the xpath i wrote.这是我编写的 html 元素和 xpath。
<div class="results searchResults" style="display: block;”>
<table cellspacing="0" id="resultGroup">
<colgroup>
<col width="4.216%">
<col width="43.13%">
<col width="52.65%">
</colgroup>
<tbody>
<tr class="">
<td class="selector">
<a href="#" id="c_2441797" title="Apple Inc." class="checkbox unchecked"> </a>
</td>
<td class="name">
<div>
<a href="#!search/profile/company?companyId=2441797&targetid=profile" class="companyResultsName">Apple Inc.</a>
<a href="http://www.google.com/finance?client=ob&q=NASDAQ: AAPL" rel="external" target="_blank">(NASDAQ: AAPL)</a>
</div>
the xpath is self.driver.find_element_by_xpath(".//*[@id='resultGroup']/tbody/tr[@class='name']/div[1]/a[@class='companyResultsName']").click()
xpath 是self.driver.find_element_by_xpath(".//*[@id='resultGroup']/tbody/tr[@class='name']/div[1]/a[@class='companyResultsName']").click()
hope someone could give me a hint.希望有人能给我一个提示。 Thanks.谢谢。
If your element is dynamically generated by JavaScript
, you should wait until it present in DOM
:如果你的元素是由JavaScript
动态生成的,你应该等到它出现在DOM
:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
link = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, "companyResultsName")))
link.click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.