Selenium Web驱动程序:如何单击按钮?

时间:2018-10-09 06:51:31

标签: python selenium selenium-webdriver webdriver

<button style="margin: 8px 5px 0 0; height: 40px;" 
class="gprospect_linkedin_button gprospect_linkedin_button_style_medium"> 
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyhpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMTM4IDc5LjE1OTgyNCwgMjAxNi8wOS8xNC0wMTowOTowMSAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENDIDIwMTcgKE1hY2ludG9zaCkiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6RUM5NDk2ODA2NjYzMTFFOEIxMDBFQzUxNzIzRTMwQTMiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6RUM5NDk2ODE2NjYzMTFFOEIxMDBFQzUxNzIzRTMwQTMiPiA8eG1wTU06RGVya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KdWZxb+m+uUgMp5t2Z2q208la2W5ONDlDUrzA29VM8nP/uwfwMY3SxsoWwJY7gQOD0DjWee7VB9yQwD/doDvIDcMIOMbpM4duSAgSsLt0QaiwGWb1LXDzYczHSh9m9L88LgAhB+ZuS+TL2QzUXsGuBs4WECGH5Q6PZvpF7MdKfIw4VbK9peluRXWYYaroU4OCYgOvJ4m8fTYxutg+EZ57af70jHk8tFZVib66OyoPBndDmyhAnt0NlH+bx+eTiY88ZtN4vF5vv3m5vF5xlGghcSj802Ux8fn/yfAAPcyDHXQoP0GAAAAAElFTkSuQmCC">Get email
</button>

尝试以下代码,但找不到元素:

driver.find_element_by_class_name('gprospect_linkedin_button').click()

4 个答案:

答案 0 :(得分:0)

实际的类名称为'gprospect_linkedin_button gprospect_linkedin_button_style_medium'

find_element_by_class_name 函数旨在通过完整的类名获取元素。

您可以尝试使用xpath这样定位它:

//button[contains(@class, 'gprospect_linkedin_button')];

答案 1 :(得分:0)

您可以使用xpath starts-with函数来解决此问题。 喜欢:

//*[starts-with(., 'gprospect_linkedin_button')]

答案 2 :(得分:0)

根据配置详细信息(在您的comment更新中)...

(Session info: chrome=69.0.3497.100) 
(Driver info: chromedriver=2.37.543627 (63642262d9fb93fb4ab52398be4286d844092a5e),platform=Windows NT 6.3.9600 x86_64) 

您的主要问题是所使用的二进制版本之间的不兼容性

  • 您正在使用 chromedriver = 2.37
  • chromedriver=2.37的发行说明中明确提到以下内容:
  

支持 Chrome v64-66

  • 您正在使用 chrome = 69.0
  • ChromeDriver v2.42的发行说明中明确提到以下内容:
  

支持 Chrome v68-70

因此 ChromeDriver v2.37 Chrome浏览器v69.0

之间存在明显的不匹配

解决方案

  • ChromeDriver 升级到当前的ChromeDriver v2.42级别。
  • Chrome 版本保持在 Chrome v68-70 级别之间。 (as per ChromeDriver v2.42 release notes
  • 通过您的 IDE
  • 清理您的项目工作区重建您的项目,并且仅具有必需的依赖项。
  • 如果您的基本 Web客户端版本过旧,请通过Revo Uninstaller进行卸载,并安装最新版本的 Web客户端。 li>
  • 使用以下代码行在所需元素上调用click()

    driver.find_element_by_xpath("//button[@class='gprospect_linkedin_button gprospect_linkedin_button_style_medium'][normalize-space()='Get email']").click()
    
  • 执行您的@Test


更新A

二进制更新后您仍然看到NoSuchElementException,因此需要诱使 WebDriverWait ,使所需的元素可点击,如下所示:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# other lines of code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='gprospect_linkedin_button gprospect_linkedin_button_style_medium'][normalize-space()='Get email']"))).click()

更新B

由于您仍然无法click(),因此需要使用execute_script()方法,如下所示:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# other lines of code
myElement = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='gprospect_linkedin_button gprospect_linkedin_button_style_medium'][normalize-space()='Get email']")))
driver.execute_script("arguments[0].click();", myElement)

答案 3 :(得分:0)

尝试一下:

OnePlus 6