如何使用以下代码编写单击按钮代码

时间:2019-06-13 19:26:56

标签: selenium-webdriver

如何在Selenium Webdriver中使用以下代码编写单击按钮代码

<div id="leftblock">
               <div id="leftheader">Dell Inspiron 17R </div>
                 <div id="leftcontent"><div style="float:left;padding-right:3px;padding-bottom:3px;">
                            <img src="images/product/midium/Dell110001.jpg" border="0" width="80" />
                 </div>Dell Inspiron 17R Special Edition 3rd Gen Intel Core i7-3630QM Quad Core 17.3" Laptop, 8GB/1TB/Win 8/2GB Video
                <p class="pricetag">&pound;1000.00</p>
                <p class="byline"><a href="proddetail.php?proid=10001" class="aa">
                <img src="images/buynow.jpg" border="0" /></a></p></div></div>

1 个答案:

答案 0 :(得分:0)

element=driver.find_element_by_xpath(" //*[@id='desktopSearchResults']/div[2]/section/div[2]/ul/li[12]/a")
    element.click()

如果您想尝试我的完整代码,则:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
import random


from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys




seed = 1
random.seed(seed)

driver = webdriver.Chrome()
driver.get("https://www.myntra.com/")

element = driver.find_element_by_xpath("//*[@id='desktop-header-cnt']/div[2]/div[3]/input")

# Put the word "history" in the search box and hit enter
element.send_keys("pantaloons")
element.send_keys(Keys.RETURN)

time.sleep(3)
for i in range(1000):
    time.sleep(1)
    for i in range(120):
        actions = ActionChains(driver)
        actions.send_keys(Keys.ARROW_DOWN)
        actions.perform()
        time.sleep(0.10)

    element=driver.find_element_by_xpath(" //*[@id='desktopSearchResults']/div[2]/section/div[2]/ul/li[12]/a")
    element.click()
    time.sleep(1)

您必须先获取元素,然后调用click()方法。元素可以通过chrome中的ctrl + shift + i轻松获取,并将鼠标悬停在按钮上,在源代码中,单击copy> xaml