是否有任何方法可以使用python在selenium中单击svg元素?
实际上,我正试图在PyCharm中使用Selenium制作一个是否有任何方法可以使用python在selenium中单击svg元素?,python,selenium,svg,Python,Selenium,Svg,实际上,我正试图在PyCharm中使用Selenium制作一个instabot,并尝试点击like按钮,但由于它的svg属性,我无法触发like按钮,如果有人知道如何点击like按钮,请帮助我 代码如下: def findhashtag(self): self.driver.get('https://www.instagram.com/explore/tags/'+self.hashtag+'/') like_picture = self.driver.find_element_
instabot
,并尝试点击like按钮,但由于它的svg属性,我无法触发like按钮,如果有人知道如何点击like按钮,请帮助我
代码如下:
def findhashtag(self):
self.driver.get('https://www.instagram.com/explore/tags/'+self.hashtag+'/')
like_picture = self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[1]/a/div')
like_picture.click() #this is to open picture
elememnt = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(("xpath", '//*[local-name()="svg"][@class="_8-yf5"]')))
#self.driver.find_element_by_xpath('//button[@class="wp06b"]//*[name()="svg"]').click() #another method to click on that
#self.driver.find_element_by_xpath('//*[name()="svg"]//*[name()="viewBox" and @class="_8-yf5")').click() #another method to click on that
#self.driver.find_element_by_link_text("Like").click() #another method to click on that
follow = WebDriverWait(self.driver, 15).until(lambda d: d.find_element_by_xpath('//button[text()="Follow"]'))
follow.click() #this is to follow and its working fine
try:
like_button = WebDriverWait(self.driver, 15).until(lambda d: d.find_element_by_xpath('//button[@class="wp06b"]//*[name()="svg"]'))
like_button.click() #trying to clic[enter image description here][1]k on like button
except:
print("not liked")
也许如果您将“通过XPath查找元素”更改为可单击的元素
from selenium.webdriver.support import expected_conditions as EC
try:
WebDriverWait(self.driver, 15).until(EC.element_to_be_clickable((By.XPATH, '#XPATH here'))).click()
except:
print('this did not work')
确实有帮助。SVG元素没有onclick方法,但您可以用javascript解决这个问题。