单击<a> tag

时间:2018-12-14 08:03:47

标签: python selenium-webdriver beautifulsoup

Hi i have the following in python

#Searching for company
varA = soup.find(Microsoft)
#Finding the <a> tag which contains href
#{<a data-deptmodal="true" href="https://someURL BASED ON COMPANY NAME">TEXT BASED ON COMPANY NAME</a>}
button = org.find_previous('a')
driver.find_element_by_tag_name(button).click()

and i get an error like

TypeError: Object of type 'Tag' is not JSON serializable

How do I make the webdriver click on my href after i get the soup please note that my href changes everytime i change the company name.

1 个答案:

答案 0 :(得分:1)

要添加到现有注释中,BeautifulSoup是一个 HTML解析器,它可以帮助您从HTML中提取数据,它不会以任何方式与页面进行交互-它不能,例如,单击链接。

如果需要在浏览器中单击链接,请通过selenium进行操作。在您的情况下,.find_element_by_link_text()(或.find_element_by_partial_link_text())定位器非常适合此问题:

driver.find_element_by_link_text("Microsoft")

文档参考:Locating Hyperlinks by Link Text