如何通过Selenium和Python在Instagram中单击“不立即”按钮

时间:2018-12-09 07:03:12

标签: python selenium xpath css-selectors instagram

我正在尝试使用当前正在编码的“ instagram机器人”登录Instagram。我已经通过登录屏幕,通过“获取应用程序”屏幕,以及如何打开通知。这两个选项是“打开”和“不立即”。我正在尝试使用与以前相同的方法来单击“不是现在”,但无法正常工作。代码(在firefox上使用inspect元素)表示

<button class="aOOlW   HoLwm " tabindex="0">Not Now</button>

我尝试将类代码与

一起使用
notNowButton = driver.find_element_by_xpath("//a[@class='aOOlW HoLwm']")
# or
notNowButton = driver.find_element_by_xpath("//a[@tabindex='0']")

但这也不起作用。有帮助吗?

通知:

enter image description here

6 个答案:

答案 0 :(得分:0)

使用G = nx.Graph() G.add_edges_from(t) #Now, you need to find "new" neighbours for all possible combinations that make the longest chain in your case def findPaths(G, current_node, n, to_exclude = None): if to_exclude == None: to_exclude = set([current_node]) else: to_exclude.add(current_node) if n==1: return [[current_node]] paths = [[current_node]+path for neighbor in G.neighbors(current_node) if neighbor not in to_exclude for path in findPaths(G,neighbor,n-1,to_exclude)] to_exclude.remove(current_node) return paths allpaths = [] for node in G: allpaths.extend(findPaths(G, node, G.number_of_nodes())) if allpaths: print('match found') [print(x) for x in allpaths] else: print('no matches') 和Xpath匹配文本WebDriverWait

Not Now

答案 1 :(得分:0)

好像你很近。

在您的第一次代码试用中:

driver.find_element_by_xpath("//a[@class='aOOlW HoLwm']")

<tag>不是<a>标签,而是 <button> 标签。因此,按以下所述进行更改将奏效。

driver.find_element_by_xpath("//button[@class='aOOlW   HoLwm ']")

现在所需的元素是JavaScript启用的元素,因此您必须诱使 WebDriverWait 使元素可点击,并且以下行会正常工作:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='aOOlW   HoLwm ']"))).click()

作为替代方案,您还可以按如下方式使用 cssSelector

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.aOOlW.HoLwm"))).click()

注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

答案 2 :(得分:0)

browser = webdriver.Chrome('/Users/username/Documents/WebDriver/chromedriver')


browser.find_elements_by_xpath("//button[contains(text(), 'Not Now')]")[0].click()

答案 3 :(得分:0)

您必须首先使用copy css选择器复制路径

Caused by: java.time.format.DateTimeParseException: Text '2020-1-7' could not be parsed at index 5
    at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    at java.time.LocalDate.parse(LocalDate.java:400)
    at java.time.LocalDate.parse(LocalDate.java:385)

答案 4 :(得分:0)

首先,通过其文本查找元素并将其分配给not_now_button

not_now_button = driver.find_element_by_xpath("//*[text()='Not Now']")

然后单击按钮:

not_now_button.click()

答案 5 :(得分:0)

所以这对我有用。我已经对其进行了十次测试,但这可能不是最好的方法。

#we just set a simple sleep time.it's not complicated we just wait for 5 seconds to make sure the page is fully loaded
sleep(5)
#since it's a button we just write //button and then it has to search for the described class which in this scenario it is aOOlW   HoLwm and then it just clicks on it
not_now_notif=browser.find_element_by_xpath("//button[@class='aOOlW   HoLwm ']")

not_now_notif.click()

您可以在这里查看。我相信这也可能会有所帮助 https://www.edureka.co/community/575/need-wait-until-page-completely-loaded-selenium-webdriver