Selenium python在点(X,Y)不是可单击的元素,而是其他元素将接收单击(这里的元素既不是按钮也不是链接)

2024-04-16 04:19:00 发布

您现在位置:Python中文网/ 问答频道 /正文

This是指向我要爬网的电子商务网站的链接。我正在寻找一种方法,点击最有帮助,积极,消极,最近和通过认证的买家部分和刮的价值。请注意,它不是一个按钮,所以ActionChains和Javascript代码无法处理它。在

我想通过使用click或其他方法从一个移动到另一个。我尝试使用javascript执行器和ActionChains,但是我无法得到它。在

为此,我的Xpath是:

path = '//div[@class="o5jqS-"]/div[X]//div[contains(@class,"_3MuAT6")]'

它实际上返回一个元素。“X”值在循环中替换为1到5。1表示“最有帮助”,5表示“经认证的买家”

我的代码如下:

^{pr2}$

Tags: 方法代码div网站链接javascriptthis按钮
1条回答
网友
1楼 · 发布于 2024-04-16 04:19:00

你可以在页面对象上使用我的代码库,我已经试过了

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select


class SeleniumBaseClass(object):
    def __init__(self,driver):
        self.driver = driver
    def open(self,URL):
        self.driver.get(URL)
    def driverURLChange(self,URL):  
        print("change URL" + URL)
        self.driver.get(URL)
    def currentUrl(self):
        print("URL   " +  self.driver.current_url)
        return self.driver.current_url

    def locateElement(self, loc):
        try:
            print(loc)
            element = WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(loc))
            return element
        except:
            print ("cannot find {0} element".format(loc))
        return None

    def waitForElementInvisible(self,loc):
        #load-spinner
        try:
            element = WebDriverWait(self.driver,10).until(EC.invisibility_of_element_located(loc))
            return True
        except:
            print ("cannot invisibility_of_element {0} element".format(loc))
        return False            

    def send_key_with_Element(self,loc,value):
        self.locateElement(loc).clear()
        self.locateElement(loc).send_keys(value)
    def click_with_Element(self,loc):
        self.locateElement(loc).click()
    def clickElementsBySendKey(self,loc,value):
        self.locateElement(loc).send_keys(value)  




customdriver = SeleniumBaseClass(webdriver.Chrome())

customdriver.open("https://www.flipkart.com/sony-mdr-zx110-wired-headphones/p/itmehuh6zm9s7kgz?pid=ACCDZRSEYPFHAT76&srno=s_1_1&otracker=search&lid=LSTACCDZRSEYPFHAT76TANM1F&qH=a684a6245806d98f")
HelpfulTab = (By.XPATH,"//div[contains(text(),'Most Helpful')]")
PositiveTab = (By.XPATH,"//div[contains(text(),'Positive')]")

customdriver.click_with_Element(PositiveTab)

相关问题 更多 >